def run_example(with_plots=True): """ This is the same example from the Sundials package (idasRoberts_FSA_dns.c) This simple example problem for IDA, due to Robertson, is from chemical kinetics, and consists of the following three equations:: dy1/dt = -p1*y1 + p2*y2*y3 dy2/dt = p1*y1 - p2*y2*y3 - p3*y2**2 0 = y1 + y2 + y3 - 1 """ def f(t, y, yd, p): res1 = -p[0]*y[0]+p[1]*y[1]*y[2]-yd[0] res2 = p[0]*y[0]-p[1]*y[1]*y[2]-p[2]*y[1]**2-yd[1] res3 = y[0]+y[1]+y[2]-1 return N.array([res1,res2,res3]) #The initial conditons y0 = [1.0, 0.0, 0.0] #Initial conditions for y yd0 = [0.1, 0.0, 0.0] #Initial conditions for dy/dt p0 = [0.040, 1.0e4, 3.0e7] #Initial conditions for parameters #Create an Assimulo implicit problem imp_mod = Implicit_Problem(f, y0, yd0,p0=p0) #Create an Assimulo implicit solver (IDA) imp_sim = IDA(imp_mod) #Create a IDA solver #Sets the paramters imp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6]) imp_sim.algvar = [1.0,1.0,0.0] imp_sim.suppress_alg = False #Suppres the algebraic variables on the error test imp_sim.continuous_output = True #Store data continuous during the simulation imp_sim.pbar = p0 imp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test. #Let Sundials find consistent initial conditions by use of 'IDA_YA_YDP_INIT' imp_sim.make_consistent('IDA_YA_YDP_INIT') #Simulate t, y, yd = imp_sim.simulate(4,400) #Simulate 4 seconds with 400 communication points print imp_sim.p_sol[0][-1] , imp_sim.p_sol[1][-1], imp_sim.p_sol[0][-1] #Basic test nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4) nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4) nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4) nose.tools.assert_almost_equal(imp_sim.p_sol[0][-1][0], -1.8761, 2) #Values taken from the example in Sundials nose.tools.assert_almost_equal(imp_sim.p_sol[1][-1][0], 2.9614e-06, 8) nose.tools.assert_almost_equal(imp_sim.p_sol[2][-1][0], -4.9334e-10, 12) #Plot if with_plots: P.plot(t,y) P.show()
def simulate(self, tmax, nout=500, maxsteps=5000, rtol=1e-8, verbose=False, test_jac_args=None): if not hasattr(self, 't0'): self.assemble(verbose) if test_jac_args is not None: self.test_jacobian(*test_jac_args) sim = IDA(self) flag, _, _ = sim.make_consistent('IDA_YA_YDP_INIT') translation = {0:'SUCCESS', 1:'TSTOP_RETURN', 2:'ROOT_RETURN', 99:'WARNING', -1 :'TOO_MUCH_WORK', -2 :'TOO_MUCH_ACC', -3 :'ERR_FAIL', -4 :'CONV_FAIL', -5 :'LINIT_FAIL', -6 :'LSETUP_FAIL', -7 :'LSOLVE_FAIL', -8 :'RES_FAIL', -9 :'REP_RES_ERR', -10:'RTFUNC_FAIL', -11:'CONSTR_FAIL', -12:'FIRST_RES_FAIL', -13:'LINESEARCH_FAIL', -14:'NO_RECOVERY', -20:'MEM_NULL', -21:'MEM_FAIL', -22:'ILL_INPUT', -23:'NO_MALLOC', -24:'BAD_EWT', -25:'BAD_K', -26:'BAD_T', -27:'BAD_DKY'} if flag < 0: raise ArithmeticError('make_consistent failed with flag = IDA_%s' % translation[flag]) if flag != 0: warn('make_consistent returned IDA_%s' % translation[flag]) sim.rtol = rtol sim.maxsteps = maxsteps T,Y,Yd = sim.simulate(tmax, nout) ncnt = len(self.nodes) nterm = len(self.terminals) self.T = T Vs = Y [:, :ncnt] dVsdt = Yd[:, :ncnt] Is = Y [:, ncnt:(ncnt+nterm)] dIsdt = Yd[:, ncnt:(ncnt+nterm)] Qs = Y [:, (ncnt+nterm):] dQsdt = Yd[:, (ncnt+nterm):] for i,node in enumerate(self.nodes): for term in node: term.V = Vs[:, i] term.dVdt = dVsdt[:, i] for i,term in enumerate(self.terminals): term.I = Is[:, i] term.dIdt = dIsdt[:, i] for i,state in enumerate(self.states): state.val = Qs[:, i] state.der = dQsdt[:, i]
def run_example(with_plots=True): #Defines the residual def f(t,y,yd): res_0 = yd[0]-y[2] res_1 = yd[1]-y[3] res_2 = yd[2]+y[4]*y[0] res_3 = yd[3]+y[4]*y[1]+9.82 #res_4 = y[0]**2+y[1]**2-1 res_4 = y[2]**2+y[3]**2-y[4]*(y[0]**2+y[1]**2)-y[1]*9.82 return N.array([res_0,res_1,res_2,res_3,res_4]) #Defines the jacobian def jac(c,t,y,yd): jacobian = N.zeros([len(y),len(y)]) #Derivative jacobian[0,0] = 1*c jacobian[1,1] = 1*c jacobian[2,2] = 1*c jacobian[3,3] = 1*c #Differentiated jacobian[0,2] = -1 jacobian[1,3] = -1 jacobian[2,0] = y[4] jacobian[3,1] = y[4] jacobian[4,0] = y[0]*2*y[4]*-1 jacobian[4,1] = y[1]*2*y[4]*-1-9.82 jacobian[4,2] = y[2]*2 jacobian[4,3] = y[3]*2 #Algebraic jacobian[2,4] = y[0] jacobian[3,4] = y[1] jacobian[4,4] = -(y[0]**2+y[1]**2) return jacobian #The initial conditons y0 = [1.0,0.0,0.0,0.0,5] #Initial conditions yd0 = [0.0,0.0,0.0,-9.82,0.0] #Initial conditions #Create an Assimulo implicit problem imp_mod = Implicit_Problem(f,y0,yd0) #Sets the options to the problem imp_mod.jac = jac #Sets the jacobian imp_mod.algvar = [1.0,1.0,1.0,1.0,0.0] #Set the algebraic components imp_mod.name = 'Test Jacobian' #Create an Assimulo implicit solver (IDA) imp_sim = IDA(imp_mod) #Create a IDA solver #Sets the paramters imp_sim.atol = 1e-6 #Default 1e-6 imp_sim.rtol = 1e-6 #Default 1e-6 imp_sim.suppress_alg = True #Suppres the algebraic variables on the error test #Let Sundials find consistent initial conditions by use of 'IDA_YA_YDP_INIT' imp_sim.make_consistent('IDA_YA_YDP_INIT') #Simulate t, y, yd = imp_sim.simulate(5,1000) #Simulate 5 seconds with 1000 communication points #Basic tests nose.tools.assert_almost_equal(y[-1][0],0.9401995, places=4) nose.tools.assert_almost_equal(y[-1][1],-0.34095124, places=4) nose.tools.assert_almost_equal(yd[-1][0], -0.88198927, places=4) nose.tools.assert_almost_equal(yd[-1][1], -2.43227069, places=4) #Plot if with_plots: P.plot(t,y) P.show()
def run_example(with_plots=True): """ This is the same example from the Sundials package (idasRoberts_FSA_dns.c) This simple example problem for IDA, due to Robertson, is from chemical kinetics, and consists of the following three equations:: dy1/dt = -p1*y1 + p2*y2*y3 dy2/dt = p1*y1 - p2*y2*y3 - p3*y2**2 0 = y1 + y2 + y3 - 1 """ def f(t, y, yd, p): res1 = -p[0] * y[0] + p[1] * y[1] * y[2] - yd[0] res2 = p[0] * y[0] - p[1] * y[1] * y[2] - p[2] * y[1]**2 - yd[1] res3 = y[0] + y[1] + y[2] - 1 return N.array([res1, res2, res3]) #The initial conditons y0 = [1.0, 0.0, 0.0] #Initial conditions for y yd0 = [0.1, 0.0, 0.0] #Initial conditions for dy/dt p0 = [0.040, 1.0e4, 3.0e7] #Initial conditions for parameters #Create an Assimulo implicit problem imp_mod = Implicit_Problem(f, y0, yd0, p0=p0) #Create an Assimulo implicit solver (IDA) imp_sim = IDA(imp_mod) #Create a IDA solver #Sets the paramters imp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6]) imp_sim.algvar = [1.0, 1.0, 0.0] imp_sim.suppress_alg = False #Suppres the algebraic variables on the error test imp_sim.continuous_output = True #Store data continuous during the simulation imp_sim.pbar = p0 imp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test. #Let Sundials find consistent initial conditions by use of 'IDA_YA_YDP_INIT' imp_sim.make_consistent('IDA_YA_YDP_INIT') #Simulate t, y, yd = imp_sim.simulate( 4, 400) #Simulate 4 seconds with 400 communication points print imp_sim.p_sol[0][-1], imp_sim.p_sol[1][-1], imp_sim.p_sol[0][-1] #Basic test nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4) nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4) nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4) nose.tools.assert_almost_equal( imp_sim.p_sol[0][-1][0], -1.8761, 2) #Values taken from the example in Sundials nose.tools.assert_almost_equal(imp_sim.p_sol[1][-1][0], 2.9614e-06, 8) nose.tools.assert_almost_equal(imp_sim.p_sol[2][-1][0], -4.9334e-10, 12) #Plot if with_plots: P.plot(t, y) P.show()
def run_example(with_plots=True): #Defines the residual def f(t, y, yd): res_0 = yd[0] - y[2] res_1 = yd[1] - y[3] res_2 = yd[2] + y[4] * y[0] res_3 = yd[3] + y[4] * y[1] + 9.82 #res_4 = y[0]**2+y[1]**2-1 res_4 = y[2]**2 + y[3]**2 - y[4] * (y[0]**2 + y[1]**2) - y[1] * 9.82 return N.array([res_0, res_1, res_2, res_3, res_4]) #Defines the jacobian def jac(c, t, y, yd): jacobian = N.zeros([len(y), len(y)]) #Derivative jacobian[0, 0] = 1 * c jacobian[1, 1] = 1 * c jacobian[2, 2] = 1 * c jacobian[3, 3] = 1 * c #Differentiated jacobian[0, 2] = -1 jacobian[1, 3] = -1 jacobian[2, 0] = y[4] jacobian[3, 1] = y[4] jacobian[4, 0] = y[0] * 2 * y[4] * -1 jacobian[4, 1] = y[1] * 2 * y[4] * -1 - 9.82 jacobian[4, 2] = y[2] * 2 jacobian[4, 3] = y[3] * 2 #Algebraic jacobian[2, 4] = y[0] jacobian[3, 4] = y[1] jacobian[4, 4] = -(y[0]**2 + y[1]**2) return jacobian #The initial conditons y0 = [1.0, 0.0, 0.0, 0.0, 5] #Initial conditions yd0 = [0.0, 0.0, 0.0, -9.82, 0.0] #Initial conditions #Create an Assimulo implicit problem imp_mod = Implicit_Problem(f, y0, yd0) #Sets the options to the problem imp_mod.jac = jac #Sets the jacobian imp_mod.algvar = [1.0, 1.0, 1.0, 1.0, 0.0] #Set the algebraic components imp_mod.name = 'Test Jacobian' #Create an Assimulo implicit solver (IDA) imp_sim = IDA(imp_mod) #Create a IDA solver #Sets the paramters imp_sim.atol = 1e-6 #Default 1e-6 imp_sim.rtol = 1e-6 #Default 1e-6 imp_sim.suppress_alg = True #Suppres the algebraic variables on the error test #Let Sundials find consistent initial conditions by use of 'IDA_YA_YDP_INIT' imp_sim.make_consistent('IDA_YA_YDP_INIT') #Simulate t, y, yd = imp_sim.simulate( 5, 1000) #Simulate 5 seconds with 1000 communication points #Basic tests nose.tools.assert_almost_equal(y[-1][0], 0.9401995, places=4) nose.tools.assert_almost_equal(y[-1][1], -0.34095124, places=4) nose.tools.assert_almost_equal(yd[-1][0], -0.88198927, places=4) nose.tools.assert_almost_equal(yd[-1][1], -2.43227069, places=4) #Plot if with_plots: P.plot(t, y) P.show()