def run_example(with_plots=True): r""" Example for the use of LSODAR method to solve Van der Pol's equation .. math:: \dot y_1 &= y_2 \\ \dot y_2 &= \mu ((1.-y_1^2) y_2-y_1) with :math:`\mu=\frac{1}{5} 10^3`. on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ #Define the rhs def f(t,y): eps = 1.e-6 my = 1./eps yd_0 = y[1] yd_1 = my*((1.-y[0]**2)*y[1]-y[0]) return N.array([yd_0,yd_1]) y0 = [2.0,-0.6] #Initial conditions #Define an Assimulo problem exp_mod = Explicit_Problem(f,y0, name = "LSODAR: Van der Pol's equation") #Define an explicit solver exp_sim = LSODAR(exp_mod) #Create a Radau5 solver #Sets the parameters exp_sim.atol = 1e-4 #Default 1e-6 exp_sim.rtol = 1e-4 #Default 1e-6 #Simulate t, y = exp_sim.simulate(2.) #Simulate 2 seconds #Plot if with_plots: import pylab as P P.plot(t,y[:,0], marker='o') P.title(exp_mod.name) P.ylabel("State: $y_1$") P.xlabel("Time") P.show() #Basic test x1 = y[:,0] assert N.abs(x1[-1]-1.706168035) < 1e-3 #For test purpose return exp_mod, exp_sim
def setUp(self): """ This sets up the test case. """ def f(t, y): eps = 1.e-6 my = 1. / eps yd_0 = y[1] yd_1 = my * ((1. - y[0]**2) * y[1] - y[0]) return N.array([yd_0, yd_1]) def jac(t, y): eps = 1.e-6 my = 1. / eps J = N.zeros([2, 2]) J[0, 0] = 0. J[0, 1] = 1. J[1, 0] = my * (-2. * y[0] * y[1] - 1.) J[1, 1] = my * (1. - y[0]**2) return J def jac_sparse(t, y): eps = 1.e-6 my = 1. / eps J = N.zeros([2, 2]) J[0, 0] = 0. J[0, 1] = 1. J[1, 0] = my * (-2. * y[0] * y[1] - 1.) J[1, 1] = my * (1. - y[0]**2) return sp.csc_matrix(J) #Define an Assimulo problem y0 = [2.0, -0.6] #Initial conditions exp_mod = Explicit_Problem(f, y0) exp_mod_t0 = Explicit_Problem(f, y0, 1.0) exp_mod_sp = Explicit_Problem(f, y0) exp_mod.jac = jac exp_mod_sp.jac = jac_sparse self.mod = exp_mod #Define an explicit solver self.sim = LSODAR(exp_mod) #Create a LSODAR solve self.sim_sp = LSODAR(exp_mod_sp) #Sets the parameters self.sim.atol = 1e-6 #Default 1e-6 self.sim.rtol = 1e-6 #Default 1e-6 self.sim.usejac = False
def test_event_localizer(self): exp_mod = Extended_Problem() #Create the problem exp_sim = LSODAR(exp_mod) #Create the solver exp_sim.verbosity = 0 exp_sim.report_continuously = True #Simulate t, y = exp_sim.simulate( 10.0, 1000) #Simulate 10 seconds with 1000 communications points #Basic test nose.tools.assert_almost_equal(y[-1][0], 8.0) nose.tools.assert_almost_equal(y[-1][1], 3.0) nose.tools.assert_almost_equal(y[-1][2], 2.0)
def test_against_normal_solvers(): """ Test the HHT method on the damper system and compares the result against a simulation using LSODAR """ end = 10.0 ode = flattened_2nd_order(3, 2, 3) y0 = 3.0 v0 = 2.0 prob = Explicit_Problem(ode, [y0,v0]) exp_sim = LSODAR(prob) t, y = exp_sim.simulate(end) ode = mass_spring_damper(3, 2, 3) prob = Explicit_Problem(ode, y0) hht = HHT(prob, v0, alpha = -0.2) t, y_2 = hht.simulate(end) nose.tools.assert_almost_equal(y[-1][0]/y[-1][0], y_2[-1]/y[-1][0],places = 1)
def test_pend_newmark_against_normal(): """ Test out newmark solver againts the pendulum by comparing the result of a simulation against one done by assimulo""" import problems as p from matplotlib.pylab import plot, show pend = p.Pendulum2nd() ode = pend.fcn end = 15 y0 = pend.initial_condition() prob = Explicit_Problem(ode, y0[0]) nmark = Newmark(prob, y0[1], beta = .7, gamma = .5) t, y_2 = nmark.simulate(end) ode = pend.fcn_1 prob = Explicit_Problem(ode, y0) sim = LSODAR(prob) t, y_3 = sim.simulate(end) nose.tools.assert_almost_equal(y_3[-1][0]/y_3[-1][0], y_2[-1]/y_3[-1][0],places = 1)
def test_pend_hht_against_normal(): """ Test out hht solver againts the pendulum by comparing the result of a simulation against one done by assimulo""" import problems as p from matplotlib.pylab import plot, show, figure pend = p.Pendulum2nd() ode = pend.fcn end = 15 y0 = pend.initial_condition() prob = Explicit_Problem(ode, y0[0]) hht = HHT(prob, y0[1], alpha=-0.2) t, y_1 = hht.simulate(end) ode = pend.fcn_1 prob = Explicit_Problem(ode, y0) sim = LSODAR(prob) t, y_3 = sim.simulate(end) nose.tools.assert_almost_equal(y_3[-1][0]/y_3[-1][0], y_1[-1]/y_3[-1][0],places = 1)
def run_example(with_plots=True): r""" Example of the use of Euler's method for a differential equation with a discontinuity (state event) and the need for an event iteration. on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ #Create an instance of the problem exp_mod = Extended_Problem() #Create the problem exp_sim = LSODAR(exp_mod) #Create the solver exp_sim.verbosity = 0 exp_sim.continuous_output = True #Simulate t, y = exp_sim.simulate( 10.0, 100) #Simulate 10 seconds with 1000 communications points #Plot if with_plots: import pylab as P P.plot(t, y) P.title("Solution of a differential equation with discontinuities") P.ylabel('States') P.xlabel('Time') P.show() return exp_mod, exp_sim #Basic test nose.tools.assert_almost_equal(y[-1][0], 8.0) nose.tools.assert_almost_equal(y[-1][1], 3.0) nose.tools.assert_almost_equal(y[-1][2], 2.0)
def run_example(with_plots=True): """ Bouncing ball example to demonstrate LSODAR's discontinuity handling. Also a way to use :program:`problem.initialize` and :program:`problem.handle_result` in order to provide extra information is demonstrated. The governing differential equation is .. math:: \\dot y_1 &= y_2\\\\ \\dot y_2 &= -9.81 and the switching functions are .. math:: \\mathrm{event}_0 &= y_1 \\;\\;\\;\\text{ if } \\;\\;\\;\\mathrm{sw}_0 = 1\\\\ \\mathrm{event}_1 &= y_2 \\;\\;\\;\\text{ if }\\;\\;\\; \\mathrm{sw}_1 = 1 otherwise the events are deactivated by setting the respective value to something different from 0. The event handling sets :math:`y_1 = - 0.88 y_1` and :math:`\\mathrm{sw}_1 = 1` if the first event triggers and :math:`\\mathrm{sw}_1 = 0` if the second event triggers. """ #Create an instance of the problem exp_mod = Extended_Problem() #Create the problem exp_sim = LSODAR(exp_mod) #Create the solver exp_sim.atol = 1.e-8 exp_sim.report_continuously = True exp_sim.verbosity = 30 #Simulate t, y = exp_sim.simulate(10.0) #Simulate 10 seconds #Plot if with_plots: P.subplot(221) P.plot(t, y) P.title('LSODAR Bouncing ball (one step mode)') P.ylabel('States: $y$ and $\dot y$') P.subplot(223) P.plot(exp_sim.t_sol, exp_sim.h_sol) P.title('LSODAR step size plot') P.xlabel('Time') P.ylabel('Sepsize') P.subplot(224) P.plot(exp_sim.t_sol, exp_sim.nq_sol) P.title('LSODAR order plot') P.xlabel('Time') P.ylabel('Order') P.suptitle(exp_mod.name) P.show() return exp_mod, exp_sim
class Test_LSODAR: """ Tests the LSODAR solver. """ def setUp(self): """ This sets up the test case. """ def f(t, y): eps = 1.e-6 my = 1. / eps yd_0 = y[1] yd_1 = my * ((1. - y[0]**2) * y[1] - y[0]) return N.array([yd_0, yd_1]) def jac(t, y): eps = 1.e-6 my = 1. / eps J = N.zeros([2, 2]) J[0, 0] = 0. J[0, 1] = 1. J[1, 0] = my * (-2. * y[0] * y[1] - 1.) J[1, 1] = my * (1. - y[0]**2) return J def jac_sparse(t, y): eps = 1.e-6 my = 1. / eps J = N.zeros([2, 2]) J[0, 0] = 0. J[0, 1] = 1. J[1, 0] = my * (-2. * y[0] * y[1] - 1.) J[1, 1] = my * (1. - y[0]**2) return sp.csc_matrix(J) #Define an Assimulo problem y0 = [2.0, -0.6] #Initial conditions exp_mod = Explicit_Problem(f, y0) exp_mod_t0 = Explicit_Problem(f, y0, 1.0) exp_mod_sp = Explicit_Problem(f, y0) exp_mod.jac = jac exp_mod_sp.jac = jac_sparse self.mod = exp_mod #Define an explicit solver self.sim = LSODAR(exp_mod) #Create a LSODAR solve self.sim_sp = LSODAR(exp_mod_sp) #Sets the parameters self.sim.atol = 1e-6 #Default 1e-6 self.sim.rtol = 1e-6 #Default 1e-6 self.sim.usejac = False @testattr(stddist=True) def test_simulation(self): """ This tests the LSODAR with a simulation of the van der pol problem. """ self.sim.simulate(1.) #Simulate 2 seconds nose.tools.assert_almost_equal(self.sim.y_sol[-1][0], -1.863646028, 4) @testattr(stddist=True) def test_setcoefficients(self): elco, tesco = dcfode(1) nose.tools.assert_almost_equal(elco[0, 2], 5. / 12., 9) # AM-2 nose.tools.assert_almost_equal(tesco[0, 2], 2., 9) # AM-2 error coeff @testattr(stddist=True) def test_readcommon(self): """ This tests the LSODAR's subroutine dsrcar (read functionality). """ self.sim.simulate(1.) #Simulate 2 seconds r = N.ones((245, ), 'd') i = N.ones((55, ), 'i') dsrcar(r, i, 1) nose.tools.assert_almost_equal(r[217], 2.22044605e-16, 20) nose.tools.assert_equal(i[36], 3) @testattr(stddist=True) def test_writereadcommon(self): """ This tests the LSODAR's subroutine dsrcar (write and read functionality). """ r = N.ones((245, ), 'd') i = N.ones((55, ), 'i') dsrcar(r, i, 2) r[0] = 100. i[0] = 10 dsrcar(r, i, 1) nose.tools.assert_almost_equal(r[0], 1., 4) nose.tools.assert_equal(i[0], 1) def test_rkstarter(self): """ This test checks the correctness of the Nordsieck array generated from a RK starter """ pass """ A=N.array([[0.,1.],[-4.,0.]]) def f(t,x,sw0): return N.dot(A,N.array(x)) H = 1.e-8 # Compute the exact solution at h=0,H/4,H/2,3H/4,H T=N.array([0.,H/4.,H/2.,3./4.*H,H]) y0=N.array([1.,0.]) from scipy.linalg import expm exact=N.array([N.dot(expm(A*t),y0) for t in T]) #polynomial interpolation from scipy import polyfit coeff = polyfit(T,exact,4) d1coeff=N.array([4,3,2,1]).reshape(-1,1)*coeff[:-1,:] d2coeff=N.array([3,2,1]).reshape(-1,1)*d1coeff[:-1,:] d3coeff=N.array([2,1]).reshape(-1,1)*d2coeff[:-1,:] d4coeff=N.array([1]).reshape(-1,1)*d3coeff[:-1,:] h=H/4. nordsieck_at_0=N.array([coeff[-1,:],h*d1coeff[-1,:],h**2/2.*d2coeff[-1,:], h**3/6.*d3coeff[-1,:],h**4/24.*d4coeff[-1,:]]) rkNordsieck=odepack.RKStarterNordsieck(f,H) computed=rkNordsieck(0,y0) numpy.testing.assert_allclose(computed[1], nordsieck_at_0, atol=H/100., verbose=True) """ @testattr(stddist=True) def test_interpol(self): # a with interpolation and report_continuously self.sim.report_continuously = True t_sol, y_sol = self.sim.simulate(1., ncp_list=[0.5]) self.sim.reset() t_sol1, y_sol1 = self.sim.simulate(0.5) ind05 = N.nonzero(N.array(t_sol) == 0.5)[0][0] #print y_sol[ind05],y_sol1[-1] nose.tools.assert_almost_equal(y_sol[ind05, 0], y_sol1[-1, 0], 6) def test_simulation_with_jac(self): """ This tests the LSODAR with a simulation of the van der pol problem. """ self.sim.usejac = True self.sim.simulate(1.) #Simulate 2 seconds nose.tools.assert_almost_equal(self.sim.y_sol[-1][0], -1.863646028, 4) @testattr(stddist=True) def test_simulation_ncp(self): self.sim.simulate(1., 100) #Simulate 2 seconds nose.tools.assert_almost_equal(self.sim.y_sol[-1][0], -1.863646028, 4) @testattr(stddist=True) def test_usejac_csc_matrix(self): self.sim_sp.usejac = True self.sim_sp.simulate(2.) #Simulate 2 seconds assert self.sim_sp.statistics["nfcnjacs"] == 0 nose.tools.assert_almost_equal(self.sim_sp.y_sol[-1][0], 1.7061680350, 4) @testattr(stddist=True) def test_simulation_ncp_list(self): self.sim.simulate(1., ncp_list=[0.5]) #Simulate 2 seconds nose.tools.assert_almost_equal(self.sim.y_sol[-1][0], -1.863646028, 4) @testattr(stddist=True) def test_maxh(self): self.sim.hmax = 1.0 assert self.sim.options["maxh"] == 1.0 assert self.sim.maxh == 1.0 self.sim.maxh = 2.0 assert self.sim.options["maxh"] == 2.0 assert self.sim.maxh == 2.0 @testattr(stddist=True) def test_simulation_ncp_list_2(self): self.sim.simulate(1., ncp_list=[0.5, 4]) #Simulate 2 seconds nose.tools.assert_almost_equal(self.sim.y_sol[-1][0], -1.863646028, 4) @testattr(stddist=True) def test_simulation_ncp_with_jac(self): """ Test a simulation with ncp. """ self.sim.usejac = True self.sim.simulate(1., 100) #Simulate 2 seconds nose.tools.assert_almost_equal(self.sim.y_sol[-1][0], -1.863646028, 4)
def _default_solver_instance(self): from assimulo.solvers import LSODAR return LSODAR(self._assimulo_problem)