# Size of variable arrays: sizeAlgebraic = 39 sizeStates = 11 sizeConstants = 48 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_constants[0] = "q_Cai_init in component environment (fmol)" legend_constants[1] = "q_TRPN_init in component environment (fmol)" legend_constants[2] = "q_Ca_TRPN_init in component environment (fmol)" legend_constants[3] = "q_B_init in component environment (fmol)" legend_constants[4] = "q_U_init in component environment (fmol)" legend_constants[5] = "q_W_init in component environment (fmol)" legend_constants[6] = "q_S_init in component environment (fmol)" legend_algebraic[2] = "q_TRPN in component environment (fmol)" legend_algebraic[5] = "q_Cai in component environment (fmol)" legend_algebraic[8] = "q_Ca_TRPN in component environment (fmol)" legend_algebraic[9] = "q_B in component environment (fmol)" legend_algebraic[10] = "q_U in component environment (fmol)" legend_algebraic[11] = "q_W in component environment (fmol)" legend_algebraic[12] = "q_S in component environment (fmol)" legend_states[0] = "q_TRPN in component crossbridge_TRPN (fmol)" legend_states[1] = "q_Cai in component crossbridge_TRPN (fmol)" legend_states[2] = "q_Ca_TRPN in component crossbridge_TRPN (fmol)" legend_states[3] = "q_B in component crossbridge_TRPN (fmol)" legend_states[4] = "q_U in component crossbridge_TRPN (fmol)" legend_states[5] = "q_W in component crossbridge_TRPN (fmol)" legend_states[6] = "q_S in component crossbridge_TRPN (fmol)" legend_algebraic[35] = "T_total in component crossbridge_TRPN (kPa)" legend_states[7] = "SL in component crossbridge_TRPN (metre)" legend_constants[7] = "kappa_R_TRPNCa in component crossbridge_TRPN_parameters (fmol_per_sec)" legend_constants[8] = "kappa_R_BU in component crossbridge_TRPN_parameters (fmol_per_sec)" legend_constants[9] = "kappa_R_UW in component crossbridge_TRPN_parameters (fmol_per_sec)" legend_constants[10] = "kappa_R_WS in component crossbridge_TRPN_parameters (fmol_per_sec)" legend_constants[11] = "kappa_R_SU in component crossbridge_TRPN_parameters (fmol_per_sec)" legend_constants[12] = "K_TRPN in component crossbridge_TRPN_parameters (per_fmol)" legend_constants[13] = "K_Cai in component crossbridge_TRPN_parameters (per_fmol)" legend_constants[14] = "K_Ca_TRPN in component crossbridge_TRPN_parameters (per_fmol)" legend_constants[15] = "K_B in component crossbridge_TRPN_parameters (per_fmol)" legend_constants[16] = "K_U in component crossbridge_TRPN_parameters (per_fmol)" legend_constants[17] = "K_W in component crossbridge_TRPN_parameters (per_fmol)" legend_constants[18] = "K_S in component crossbridge_TRPN_parameters (per_fmol)" legend_constants[19] = "R in component constants (J_per_K_per_mol)" legend_constants[20] = "T in component constants (kelvin)" legend_constants[21] = "n_Tm in component crossbridge_TRPN (dimensionless)" legend_algebraic[13] = "mu_TRPN in component crossbridge_TRPN (J_per_mol)" legend_algebraic[14] = "mu_Cai in component crossbridge_TRPN (J_per_mol)" legend_algebraic[15] = "mu_Ca_TRPN in component crossbridge_TRPN (J_per_mol)" legend_algebraic[16] = "mu_B in component crossbridge_TRPN (J_per_mol)" legend_algebraic[18] = "mu_U in component crossbridge_TRPN (J_per_mol)" legend_algebraic[20] = "mu_W in component crossbridge_TRPN (J_per_mol)" legend_algebraic[22] = "mu_S in component crossbridge_TRPN (J_per_mol)" legend_algebraic[38] = "v_R_TRPNCa in component crossbridge_TRPN (fmol_per_sec)" legend_algebraic[24] = "v_R_BU in component crossbridge_TRPN (fmol_per_sec)" legend_algebraic[30] = "v_R_UW in component crossbridge_TRPN (fmol_per_sec)" legend_algebraic[32] = "v_R_WS in component crossbridge_TRPN (fmol_per_sec)" legend_algebraic[34] = "v_R_SU in component crossbridge_TRPN (fmol_per_sec)" legend_algebraic[36] = "tension in component crossbridge_TRPN (N_per_mm2)" legend_algebraic[37] = "mu_tension in component crossbridge_TRPN (J_per_mol)" legend_constants[22] = "kf_coeff in component crossbridge_TRPN (dimensionless)" legend_constants[43] = "n in component crossbridge_TRPN (J_per_mol)" legend_constants[23] = "hh in component crossbridge_TRPN (mm2_per_N)" legend_constants[24] = "SL_0 in component crossbridge_TRPN (metre)" legend_constants[25] = "q_MS in component crossbridge_TRPN (fmol)" legend_constants[26] = "r_s in component crossbridge_TRPN (dimensionless)" legend_constants[27] = "r_w in component crossbridge_TRPN (dimensionless)" legend_constants[28] = "A_eff in component crossbridge_TRPN (dimensionless)" legend_constants[29] = "phi in component crossbridge_TRPN (dimensionless)" legend_constants[30] = "k_uw in component crossbridge_TRPN (per_sec)" legend_constants[31] = "k_ws in component crossbridge_TRPN (per_sec)" legend_states[8] = "G_w in component crossbridge_TRPN (metre)" legend_states[9] = "G_s in component crossbridge_TRPN (metre)" legend_constants[40] = "c_w in component crossbridge_TRPN (per_sec)" legend_constants[41] = "c_s in component crossbridge_TRPN (per_sec)" legend_constants[42] = "A_w in component crossbridge_TRPN (dimensionless)" legend_constants[44] = "A_s in component crossbridge_TRPN (dimensionless)" legend_algebraic[3] = "mu_1 in component crossbridge_TRPN (J_per_m)" legend_constants[45] = "mu_2 in component crossbridge_TRPN (J_per_m)" legend_algebraic[0] = "mu_3 in component crossbridge_TRPN (J_per_m)" legend_algebraic[4] = "mu_4 in component crossbridge_TRPN (J_per_m)" legend_constants[46] = "mu_5 in component crossbridge_TRPN (J_per_m)" legend_algebraic[1] = "mu_6 in component crossbridge_TRPN (J_per_m)" legend_algebraic[6] = "v_1 in component crossbridge_TRPN (m_per_s)" legend_constants[32] = "v_2 in component crossbridge_TRPN (m_per_s)" legend_algebraic[7] = "v_4 in component crossbridge_TRPN (m_per_s)" legend_constants[33] = "v_to_mu in component crossbridge_TRPN (Js_per_m2)" legend_algebraic[23] = "T_active in component crossbridge_TRPN (kPa)" legend_constants[34] = "T_ref in component crossbridge_TRPN (kPa)" legend_algebraic[21] = "mu_T_a in component crossbridge_TRPN (kPa)" legend_algebraic[17] = "mu_T_S in component crossbridge_TRPN (kPa)" legend_algebraic[19] = "mu_T_W in component crossbridge_TRPN (kPa)" legend_states[10] = "Cdd in component crossbridge_TRPN (metre)" legend_algebraic[25] = "eta in component crossbridge_TRPN (per_sec)" legend_constants[35] = "eta_l in component crossbridge_TRPN (per_sec)" legend_constants[36] = "eta_s in component crossbridge_TRPN (per_sec)" legend_constants[37] = "k in component crossbridge_TRPN (dimensionless)" legend_constants[38] = "alpha in component crossbridge_TRPN (kPa)" legend_algebraic[29] = "v_Cdd in component crossbridge_TRPN (m_per_s)" legend_algebraic[26] = "mu_d in component crossbridge_TRPN (J_per_m)" legend_algebraic[27] = "mu_k in component crossbridge_TRPN (J_per_m)" legend_algebraic[28] = "mu_f in component crossbridge_TRPN (J_per_m)" legend_algebraic[31] = "mu_T_passive in component crossbridge_TRPN (kPa)" legend_algebraic[33] = "T_passive in component crossbridge_TRPN (kPa)" legend_constants[39] = "F in component constants (C_per_mol)" legend_rates[1] = "d/dt q_Cai in component crossbridge_TRPN (fmol)" legend_rates[0] = "d/dt q_TRPN in component crossbridge_TRPN (fmol)" legend_rates[2] = "d/dt q_Ca_TRPN in component crossbridge_TRPN (fmol)" legend_rates[3] = "d/dt q_B in component crossbridge_TRPN (fmol)" legend_rates[4] = "d/dt q_U in component crossbridge_TRPN (fmol)" legend_rates[5] = "d/dt q_W in component crossbridge_TRPN (fmol)" legend_rates[6] = "d/dt q_S in component crossbridge_TRPN (fmol)" legend_rates[8] = "d/dt G_w in component crossbridge_TRPN (metre)" legend_rates[7] = "d/dt SL in component crossbridge_TRPN (metre)" legend_rates[9] = "d/dt G_s in component crossbridge_TRPN (metre)" legend_rates[10] = "d/dt Cdd in component crossbridge_TRPN (metre)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 6.82e-1 constants[1] = 2.57 constants[2] = 1e-6 constants[3] = 1 constants[4] = 0 constants[5] = 0 constants[6] = 0 states[0] = 1e-16 states[1] = 1e-16 states[2] = 1e-16 states[3] = 1e-16 states[4] = 1e-16 states[5] = 1e-16 states[6] = 1e-16 states[7] = 1e-6 constants[7] = 52.0453 constants[8] = 2.90875 constants[9] = 0.332501 constants[10] = 0.00782356 constants[11] = 140824 constants[12] = 1.27424 constants[13] = 1.27424 constants[14] = 0.11171 constants[15] = 6.06164 constants[16] = 2.27312 constants[17] = 14.8627 constants[18] = 3.71567e-06 constants[19] = 8.31 constants[20] = 310 constants[21] = 1.1 constants[22] = -0.0118 constants[23] = 1 constants[24] = 2e-6 constants[25] = 1e-6 constants[26] = 0.25 constants[27] = 0.5 constants[28] = 25 constants[29] = 2.23 constants[30] = 26 constants[31] = 4 states[8] = 1e-6 states[9] = 1e-6 constants[32] = 0 constants[33] = 1 constants[34] = 40.5 states[10] = 0 constants[35] = 200e3 constants[36] = 200e3 constants[37] = 7 constants[38] = 2.1 constants[39] = 96485 constants[40] = (constants[29]*constants[30]*(1.00000-constants[27]))/constants[27] constants[41] = (constants[29]*constants[31]*(1.00000-constants[26])*constants[27])/constants[26] constants[42] = (constants[28]*constants[26])/((1.00000-constants[26])*constants[27]+constants[26]) constants[43] = constants[22]*constants[19]*constants[20] constants[47] = constants[32] constants[44] = constants[42] constants[45] = -constants[42]*constants[32]*constants[33] constants[46] = -constants[44]*constants[32]*constants[33] return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic rates[7] = constants[47] algebraic[0] = constants[40]*states[8]*constants[33] algebraic[3] = -constants[45]-algebraic[0] algebraic[6] = algebraic[3]/constants[33] rates[8] = algebraic[6] algebraic[1] = constants[41]*states[9]*constants[33] algebraic[4] = -constants[46]-algebraic[1] algebraic[7] = algebraic[4]/constants[33] rates[9] = algebraic[7] algebraic[8] = states[2]+constants[2] algebraic[15] = constants[19]*constants[20]*log(constants[14]*algebraic[8]) algebraic[9] = states[3]+constants[3] algebraic[16] = constants[19]*constants[20]*log(constants[15]*algebraic[9]) algebraic[10] = states[4]+constants[4] algebraic[18] = constants[19]*constants[20]*log(constants[16]*algebraic[10]) algebraic[24] = constants[8]*(exp((algebraic[16]+constants[21]*algebraic[15])/(constants[19]*constants[20]))-exp((algebraic[18]+constants[21]*algebraic[15])/(constants[19]*constants[20]))) rates[3] = -algebraic[24] rootfind_0(voi, constants, rates, states, algebraic) algebraic[29] = algebraic[26]/constants[33] rates[10] = algebraic[29] algebraic[11] = states[5]+constants[5] algebraic[20] = constants[19]*constants[20]*log(constants[17]*algebraic[11]) algebraic[30] = constants[9]*(exp(algebraic[18]/(constants[19]*constants[20]))-exp(algebraic[20]/(constants[19]*constants[20]))) algebraic[12] = states[6]+constants[6] algebraic[22] = constants[19]*constants[20]*log(constants[18]*algebraic[12]) algebraic[32] = constants[10]*(exp(algebraic[20]/(constants[19]*constants[20]))-exp(algebraic[22]/(constants[19]*constants[20]))) rates[5] = algebraic[30]-algebraic[32] algebraic[34] = constants[11]*(exp(algebraic[22]/(constants[19]*constants[20]))-exp(algebraic[18]/(constants[19]*constants[20]))) rates[4] = (algebraic[24]-algebraic[30])+algebraic[34] rates[6] = algebraic[32]-algebraic[34] algebraic[2] = states[0]+constants[1] algebraic[13] = constants[19]*constants[20]*log(constants[12]*algebraic[2]) algebraic[5] = states[1]+constants[0] algebraic[14] = constants[19]*constants[20]*log(constants[13]*algebraic[5]) algebraic[17] = (constants[34]/(constants[24]*constants[25]*constants[26]))*states[6]*(states[9]+constants[24]) algebraic[19] = (constants[34]/(constants[24]*constants[25]*constants[26]))*states[5]*states[8] algebraic[21] = algebraic[17]+algebraic[19] algebraic[23] = algebraic[21] algebraic[31] = ((constants[38]*algebraic[25])/constants[24])*algebraic[29] algebraic[33] = algebraic[31] algebraic[35] = algebraic[23]+algebraic[33] algebraic[36] = algebraic[35]*1000.00 algebraic[37] = constants[23]*constants[43]*algebraic[36] algebraic[38] = constants[7]*(exp((algebraic[14]+algebraic[13])/(constants[19]*constants[20]))-exp((algebraic[15]+algebraic[37])/(constants[19]*constants[20]))) rates[1] = -algebraic[38] rates[0] = -algebraic[38] rates[2] = algebraic[38] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[0] = constants[40]*states[8]*constants[33] algebraic[3] = -constants[45]-algebraic[0] algebraic[6] = algebraic[3]/constants[33] algebraic[1] = constants[41]*states[9]*constants[33] algebraic[4] = -constants[46]-algebraic[1] algebraic[7] = algebraic[4]/constants[33] algebraic[8] = states[2]+constants[2] algebraic[15] = constants[19]*constants[20]*log(constants[14]*algebraic[8]) algebraic[9] = states[3]+constants[3] algebraic[16] = constants[19]*constants[20]*log(constants[15]*algebraic[9]) algebraic[10] = states[4]+constants[4] algebraic[18] = constants[19]*constants[20]*log(constants[16]*algebraic[10]) algebraic[24] = constants[8]*(exp((algebraic[16]+constants[21]*algebraic[15])/(constants[19]*constants[20]))-exp((algebraic[18]+constants[21]*algebraic[15])/(constants[19]*constants[20]))) algebraic[29] = algebraic[26]/constants[33] algebraic[11] = states[5]+constants[5] algebraic[20] = constants[19]*constants[20]*log(constants[17]*algebraic[11]) algebraic[30] = constants[9]*(exp(algebraic[18]/(constants[19]*constants[20]))-exp(algebraic[20]/(constants[19]*constants[20]))) algebraic[12] = states[6]+constants[6] algebraic[22] = constants[19]*constants[20]*log(constants[18]*algebraic[12]) algebraic[32] = constants[10]*(exp(algebraic[20]/(constants[19]*constants[20]))-exp(algebraic[22]/(constants[19]*constants[20]))) algebraic[34] = constants[11]*(exp(algebraic[22]/(constants[19]*constants[20]))-exp(algebraic[18]/(constants[19]*constants[20]))) algebraic[2] = states[0]+constants[1] algebraic[13] = constants[19]*constants[20]*log(constants[12]*algebraic[2]) algebraic[5] = states[1]+constants[0] algebraic[14] = constants[19]*constants[20]*log(constants[13]*algebraic[5]) algebraic[17] = (constants[34]/(constants[24]*constants[25]*constants[26]))*states[6]*(states[9]+constants[24]) algebraic[19] = (constants[34]/(constants[24]*constants[25]*constants[26]))*states[5]*states[8] algebraic[21] = algebraic[17]+algebraic[19] algebraic[23] = algebraic[21] algebraic[31] = ((constants[38]*algebraic[25])/constants[24])*algebraic[29] algebraic[33] = algebraic[31] algebraic[35] = algebraic[23]+algebraic[33] algebraic[36] = algebraic[35]*1000.00 algebraic[37] = constants[23]*constants[43]*algebraic[36] algebraic[38] = constants[7]*(exp((algebraic[14]+algebraic[13])/(constants[19]*constants[20]))-exp((algebraic[15]+algebraic[37])/(constants[19]*constants[20]))) 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(4)*0.1 if not iterable(voi): soln = fsolve(residualSN_0, initialGuess0, args=(algebraic, voi, constants, rates, states), xtol=1E-6) initialGuess0 = soln algebraic[25] = soln[0] algebraic[26] = soln[1] algebraic[27] = soln[2] algebraic[28] = soln[3] 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[25][i] = soln[0] algebraic[26][i] = soln[1] algebraic[27][i] = soln[2] algebraic[28][i] = soln[3] def residualSN_0(algebraicCandidate, algebraic, voi, constants, rates, states): resid = array([0.0] * 4) algebraic[25] = algebraicCandidate[0] algebraic[26] = algebraicCandidate[1] algebraic[27] = algebraicCandidate[2] algebraic[28] = algebraicCandidate[3] resid[0] = (algebraic[25]-(custom_piecewise([greater(algebraic[26] , 0.00000), constants[35] , True, constants[36]]))) resid[1] = (algebraic[27]-(constants[37]/algebraic[25])*states[10]*constants[33]) resid[2] = (algebraic[28]-(constants[37]/algebraic[25])*(states[7]-constants[24])*constants[33]) resid[3] = (algebraic[26]-(-algebraic[27]+algebraic[28])) 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)