def test_enforce_non_negativity(self): """ Test enforcement of non-negativity during integration """ # check to make sure that the answer is *incorrect* if we don't enforce # nonegativity (ineq_constr=0) y, t, ypout, t_root, y_root, i_root = daeint(non_neg_res_func, tlist_non_neg, y0_non_neg, yp0_non_neg, rtol = reltol_non_neg, atol = abstol_non_neg, ineq_constr=False) self.assertAlmostEqual(y[1][0], 0.960000000, 4) self.assertAlmostEqual(y[-4][0], -.8800000000, 4) # check to make sure that the answer is *correct* if we do enforce # nonegativity (ineq_constr=2) y, t, ypout, t_root, y_root, i_root = daeint(non_neg_res_func, tlist_non_neg, y0_non_neg, yp0_non_neg, rtol = reltol_non_neg, atol = abstol_non_neg, ineq_constr=True) self.assertAlmostEqual(y[1][0], 0.960000000, 4) self.assertAlmostEqual(y[-4][0], 0.000000, 4)
def test_redirect_output(self): """ Test to make sure we can turn output redirection on and off """ # By default output redirection is off, so we begin by doing an example # that should generate errors and making sure that no output is received. redir = Utility.Redirector() redir.start() # This example will generate errors because the maximum number of steps # (500) will be passed y, t, ypout, t_root, y_root, i_root = daeint(trig_res_func, tlist_trig, y0_trig, yp0_trig, rtol = reltol_trig, atol = abstol_trig, max_steps = 7500) messages = redir.stop() self.assertEqual(len(messages), 0) redir = Utility.Redirector() redir.start() # Now we do the same example again with output redirection off y, t, ypout, t_root, y_root, i_root = daeint(trig_res_func, tlist_trig, y0_trig, yp0_trig, rtol = reltol_trig, atol = abstol_trig, max_steps = 7500, redir_output = False) messages = redir.stop() self.assertNotEqual(len(messages), 0)
def test_algebraic_fastreactionexample(self): """ Test a dae system (algebraicRules-fastReactionExample-l2.xml) """ y, t, ypout, t_root, y_root, i_root = daeint(algExample_res_func, tlist_alg, y0_alg, yp0_alg, rtol = reltol_alg, atol = abstol_alg) self.assertAlmostEqual(y[1][0], 0.9231163463, 4) self.assertAlmostEqual(y[13][0], 0.353454681, 4) self.assertAlmostEqual(y[8][1], 0.142837751, 4) self.assertAlmostEqual(y[20][1], 0.492844600, 4) self.assertAlmostEqual(y[15][2], 0.346376313, 4) self.assertAlmostEqual(y[27][2], 0.230837103, 4) self.assertAlmostEqual(y[22][3], 0.081296859, 4) self.assertAlmostEqual(y[37][3], 0.039501126, 4) self.assertAlmostEqual(y[29][4], 0.150075280, 4) self.assertAlmostEqual(y[41][4], 0.078591978, 4) self.assertAlmostEqual(y[50][0], 0.018315639, 4) self.assertAlmostEqual(y[50][1], 0.917958431, 4) self.assertAlmostEqual(y[50][2], 0.06372593, 4) self.assertAlmostEqual(y[50][3], 0.018207409, 4) self.assertAlmostEqual(y[50][4], 0.045518522, 4)
def get_s_integration(net, p=None, Tmin=None, Tmax=None, k=None, tol=None, to_ser=False): """ """ if p is not None: net.update_optimizable_vars(p) if Tmin is None: Tmin = TMIN if Tmax is None: Tmax = TMAX if k is None: k = K if tol is None: tol = TOL_SS nsp = len(net.dynamicVars) tmin, tmax = 0, Tmin x0 = net.x0.copy() constants = net.constantVarValues while tmax <= Tmax: # yp0 helps integration stability yp0 = Dynamics.find_ics(net.x0, net.x0, tmin, net._dynamic_var_algebraic, [1e-6] * nsp, [1e-6] * nsp, constants, net)[1] # using daskr to save computational overhead out = daskr.daeint(res=net.res_function, t=[tmin, tmax], y0=x0, yp0=yp0, atol=[1e-6] * nsp, rtol=[1e-6] * nsp, intermediate_output=False, rpar=constants, max_steps=100000.0, max_timepoints=100000.0, jac=net.ddaskr_jac) xt = out[0][-1] dxdt = net.res_function(tmax, xt, [0] * nsp, constants) if np.max(np.abs(dxdt)) < tol: net.updateVariablesFromDynamicVars(xt, tmax) net.t = tmax if to_ser: return butil.Series(xt, net.xids) else: return xt else: tmin, tmax = tmax, tmax * k x0 = xt raise Exception("Cannot reach steady state for p=%s" % p)
def test_Dfun(self): """ Test user-supplied Jacobian """ y, t, ypout, t_root, y_root, i_root = daeint(vdp_res_func, tlist_vdp, y0_vdp, yp0_vdp, jac=vdp_Dfun, rtol = reltol_vdp, atol = abstol_vdp, intermediate_output=False) self.assertAlmostEqual(y[1][0], 1.85821444, 4) self.assertAlmostEqual(y[6][1], 8.93022e-3, 4)
def test_maxsteps_on(self): """ Test to make sure the max_steps parameter works """ y, t, ypout, t_root, y_root, i_root = daeint(trig_res_func, tlist_trig, y0_trig, yp0_trig, rtol = reltol_trig, atol = abstol_trig, max_steps = 7500) # the integrator will only get to the specified time points if # max_steps is increased significantly above the default self.assertAlmostEqual(y[1][0], 0.82689894, 4) self.assertAlmostEqual(y[2][0], 0.93004774, 4)
def test_algebraic_basic(self): """ Test a simpler dae system (algebraicRules-basic-l2.xml) """ y, t, ypout, t_root, y_root, i_root = daeint(algExampleBasic_res_func, tlist_algBasic, y0_algBasic, yp0_algBasic, rtol = reltol_algBasic, atol = abstol_algBasic) self.assertAlmostEqual(y[1][0], 0.590635382065755, 4) self.assertAlmostEqual(y[13][0], 0.962863096631099, 4) self.assertAlmostEqual(y[15][1], 0.0248936510867585, 4) self.assertAlmostEqual(y[27][1], 0.00225832507503575, 4)
def test_tstop(self): """ Test that integration will not continue past tstop """ y, t, ypout, t_root, y_root, i_root = daeint(linear_res_func, tlist_linear, y0_linear, yp0_linear, rtol=reltol_linear, atol=abstol_linear, tstop=tstop_linear) # Check that the final time point returned is for tstop self.assertAlmostEqual(t[-1], tstop_linear, 4) self.assertAlmostEqual(y[2][0], -100, 4)
def test_maxsteps_off(self): """ Test to make sure the trig_func problem will cause an error \ if max_steps is not set """ redir.start() try: self.assertRaises(SloppyCell.daskr.daeintException, daeint(trig_res_func, tlist_trig, y0_trig, yp0_trig, rtol = reltol_trig, atol = abstol_trig)) except SloppyCell.daskr.daeintException: pass messages = redir.stop()
def test_term_roots(self): """ Test root finding with termination """ y, t, ypout, t_root, y_root, i_root = daeint(vdp_res_func, tlist_vdp, y0_vdp, yp0_vdp, nrt=1, rt=vdp_rt_func, rtol = reltol_vdp, atol = abstol_vdp, intermediate_output=False) self.assertAlmostEqual(t_root, 0.8116351E+02, 4) self.assertAlmostEqual(y_root[0], -0.3295063E-12, 4) self.assertAlmostEqual(y_root[1], -0.6714100E+02, 3) self.assertEqual(i_root[0], -1)
def test_basic(self): """ Basic test of daskr """ y, t, ypout, t_root, y_root, i_root = daeint(vdp_res_func, tlist_vdp, y0_vdp, yp0_vdp, rtol = reltol_vdp, atol = abstol_vdp, intermediate_output=False) self.assertAlmostEqual(y[1][0], 1.85821444, 4) self.assertAlmostEqual(y[3][0], 0.1484599E+01, 4) self.assertAlmostEqual(y[7][0], -0.1501730E+01, 4) self.assertAlmostEqual(y[10][0], 0.1718428E+01, 4) self.assertAlmostEqual(y[2][1], -0.9068522E-02, 3) self.assertAlmostEqual(y[4][1], -0.5847012E-01, 3) self.assertAlmostEqual(y[8][1], 0.3569131E-01, 3) self.assertAlmostEqual(y[9][1], -0.7422161E-02, 3)
def test_algebraic_calculate_ic(self): """ Test automatic calculation of initial conditions """ # pass an inconsistent set of initial conditions to the fast reaction # example y0_inconsistent = np.array([1.0, 0, 0, 1500, 15]) yp0_inconsistent = algExample_func(y0_inconsistent, t0_alg) var_types_inconsistent = np.array([1, 1, 1, -1, -1]) y, t, ypout, t_root, y_root, i_root = daeint( algExample_res_func, tlist_alg, y0_inconsistent, yp0_alg, rtol=reltol_alg, atol=abstol_alg, calculate_ic=True, var_types=var_types_inconsistent) # check to make sure the initial condition was calculated correctly self.assertAlmostEqual(y[0][0], 1., 4) self.assertAlmostEqual(y[0][1], 0., 4) self.assertAlmostEqual(y[0][2], 0., 4) self.assertAlmostEqual(y[0][3], 0., 4) self.assertAlmostEqual(y[0][4], 0., 4) # check other points on the trajectory self.assertAlmostEqual(y[1][0], 0.9231163463, 4) self.assertAlmostEqual(y[13][0], 0.353454681, 4) self.assertAlmostEqual(y[8][1], 0.142837751, 4) self.assertAlmostEqual(y[20][1], 0.492844600, 4) self.assertAlmostEqual(y[15][2], 0.346376313, 4) self.assertAlmostEqual(y[27][2], 0.230837103, 4) self.assertAlmostEqual(y[22][3], 0.081296859, 4) self.assertAlmostEqual(y[37][3], 0.039501126, 4) self.assertAlmostEqual(y[29][4], 0.150075280, 4) self.assertAlmostEqual(y[41][4], 0.078591978, 4) self.assertAlmostEqual(y[50][0], 0.018315639, 4) self.assertAlmostEqual(y[50][1], 0.917958431, 4) self.assertAlmostEqual(y[50][2], 0.06372593, 4) self.assertAlmostEqual(y[50][3], 0.018207409, 4) self.assertAlmostEqual(y[50][4], 0.045518522, 4)
def get_s(net, p=None, method=None, Tmin=None, Tmax=None, k=None, x0=None, tol=None, to_ser=False, **kwargs_rootfinding): """ Input: method: Tmin, Tmax, k: x0: """ if p is not None: net.update_optimizable_vars(p) if method is None: method = METHOD if Tmin is None: Tmin = TMIN if method == 'integration': return get_s_integration(net, Tmin=Tmin, Tmax=Tmax, k=k, tol=tol, to_ser=to_ser) elif method == 'rootfinding': return get_s_rootfinding(net, x0=x0, tol=tol, to_ser=to_ser, **kwargs_rootfinding) elif method == 'mixed': nsp = len(net.dynamicVars) constants = net.constantVarValues yp0 = Dynamics.find_ics(net.x0, net.x0, 0, net._dynamic_var_algebraic, [1e-6] * nsp, [1e-6] * nsp, constants, net)[1] out = daskr.daeint(res=net.res_function, t=[0, Tmin], y0=net.x0.copy(), yp0=yp0, atol=[1e-6] * nsp, rtol=[1e-6] * nsp, intermediate_output=False, rpar=constants, max_steps=100000.0, max_timepoints=100000.0, jac=net.ddaskr_jac) xt = out[0][-1] return get_s_rootfinding(net, x0=xt, tol=tol, to_ser=to_ser, **kwargs_rootfinding) else: raise ValueError("Unrecognized value for method: %s" % method)