Python ( ) fabs(#expr1) #exprs[ & ] arccos(#expr1) arccosh(#expr1) arctan(1.0/#expr1) arctanh(1.0/#expr1) arcsin(1/#expr1) arcsinh(1/#expr1) arccos(1/#expr1) arccosh(1/#expr1) arcsin(#expr1) arcsinh(#expr1) arctan(#expr1) arctanh(#expr1) ceil(#expr1) cos(#expr1) cosh(#expr1) 1.0/tan(#expr1) 1.0/tanh(#expr1) 1.0/sin(#expr1) 1.0/sinh(#expr1) #lookupDiffVariable #expr1/#expr2 equal(#exprs[ , ]) exp(#expr1) 2.71828182846 factorial(#expr1) #expr1 % #expr2 == 0 False floor(#expr1) gcd_multi([#exprs[, ]]) greater_equal(#exprs[ , ]) greater(#exprs[ , ]) (not #expr1) or #expr2 defint(func#unique1, BOUND, CONSTANTS, RATES, VARIABLES, #bvarIndex)#supplement double func#unique1(double* BOUND, double* CONSTANTS, double* RATES, double* VARIABLES) { return #expr1; } lcm_multi([#exprs[, ]]) less_equal(#exprs[ , ]) log(#expr1) log(#expr1, #logbase) less(#exprs[ , ]) amax(vstack(#exprs[, ]),0) amin(vstack(#exprs[, ]),0) #expr1-#expr2 #expr1 != #expr2 !#expr1 #exprs[ | ] pi custom_piecewise([#expr1, #expr2 , #expr1, #expr2 , True, #expr1]) , True, float('nan')]) #exprs[+] power(#expr1, #expr2) floor(float(#expr1) / (#expr2)) #expr1 % #expr2 power(#expr1, 1.0/#degree) 1.0/cos(#expr1) 1.0/cosh(#expr1) sin(#expr1) sinh(#expr1) tan(#expr1) tanh(#expr1) #exprs[*] True -#expr1 #expr1*#expr2 + #expr3 #expr1*#expr2 #expr1+#expr2 (#expr1 != 0) ^ (#expr2 != 0) 0.577215664901533 float('inf') constants[%] states[%] algebraic[%] rates[%] voi 0 <LHS> = <RHS> <LHS> = <RHS> (voi, constants, rates, states, algebraic) initialGuess = None def rootfind_(voi, constants, states, algebraic): """Calculate value of algebraic variable for DAE""" from scipy.optimize import fsolve global initialGuess if initialGuess is None: initialGuess = 0.1 if not iterable(voi): = fsolve(residualSN_, initialGuess, args=(algebraic, voi, constants, rates, states), xtol=1E-6) initialGuess = else: for (i,t) in enumerate(voi): [i] = fsolve(residualSN_, initialGuess, args=(algebraic[:,i], voi[i], constants, rates, states[:,i]), xtol=1E-6) initialGuess = [i] def residualSN_(algebraicCandidate, algebraic, voi, constants, rates, states): = algebraicCandidate return () - () ]]> (voi, constants, rates, states, algebraic) initialGuess = None def rootfind_(voi, constants, rates, states, algebraic): """Calculate values of algebraic variables for DAE""" from scipy.optimize import fsolve global initialGuess if initialGuess is None: initialGuess = ones()*0.1 if not iterable(voi): soln = fsolve(residualSN_, initialGuess, args=(algebraic, voi, constants, rates, states), xtol=1E-6) initialGuess = soln = soln[] else: for (i,t) in enumerate(voi): soln = fsolve(residualSN_, initialGuess, args=(algebraic[:,i], voi[i], constants, rates[:i], states[:,i]), xtol=1E-6) initialGuess = soln [i] = soln[] def residualSN_(algebraicCandidate, algebraic, voi, constants, rates, states): resid = array([0.0] * ) = algebraicCandidate[] resid[] = () return resid ]]> # Size of variable arrays: sizeAlgebraic = sizeStates = sizeConstants = from math import * from numpy import * def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) return algebraic , , 500) # Construct ODE object to solve r = ode(computeRates) r.set_integrator('vode', method='bdf', atol=, rtol=, max_step=) 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) ]]> def createLegends(): legend_states = [""] * sizeStates legend_rates = [""] * sizeStates legend_algebraic = [""] * sizeAlgebraic legend_voi = "" legend_constants = [""] * sizeConstants return (legend_states, legend_algebraic, legend_voi, legend_constants) legend_ = " "