def setUp(self): """ This sets up the test case. """ #Define the residual def f(t, y, yd): eps = 1.e-6 my = 1. / eps yd_0 = y[1] yd_1 = my * ((1. - y[0]**2) * y[1] - y[0]) res_0 = yd[0] - yd_0 res_1 = yd[1] - yd_1 return N.array([res_0, res_1]) y0 = [2.0, -0.6] #Initial conditions yd0 = [-.6, -200000.] #Define an Assimulo problem self.mod = Implicit_Problem(f, y0, yd0) self.mod_t0 = Implicit_Problem(f, y0, yd0, 1.0) #Define an explicit solver self.sim = GLIMDA(self.mod) #Create a Radau5 solve self.sim_t0 = GLIMDA(self.mod_t0) #Sets the parameters self.sim.atol = 1e-4 #Default 1e-6 self.sim.rtol = 1e-4 #Default 1e-6 self.sim.inith = 1.e-4 #Initial step-size
def test_simulate_explicit(self): """ Test a simulation of an explicit problem using GLIMDA. """ f = lambda t, y: N.array(-y) y0 = [1.0] problem = Explicit_Problem(f, y0) simulator = GLIMDA(problem) assert simulator.yd0[0] == -simulator.y0[0] t, y = simulator.simulate(1.0) nose.tools.assert_almost_equal(float(y[-1]), float(N.exp(-1.0)), 4)
def run_example(with_plots=True): r""" Example for the use of GLIMDA (general linear multistep 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= 10^6`. on return: - :dfn:`imp_mod` problem instance - :dfn:`imp_sim` solver instance """ #Define the residual def f(t, y, yd): eps = 1.e-6 my = 1. / eps yd_0 = y[1] yd_1 = my * ((1. - y[0]**2) * y[1] - y[0]) res_0 = yd[0] - yd_0 res_1 = yd[1] - yd_1 return N.array([res_0, res_1]) y0 = [2.0, -0.6] #Initial conditions yd0 = [-.6, -200000.] #Define an Assimulo problem imp_mod = Implicit_Problem(f, y0, yd0, name='Glimbda Example: Van der Pol (implicit)') #Define an explicit solver imp_sim = GLIMDA(imp_mod) #Create a GLIMDA solver #Sets the parameters imp_sim.atol = 1e-4 #Default 1e-6 imp_sim.rtol = 1e-4 #Default 1e-6 imp_sim.inith = 1.e-4 #Initial step-size #Simulate t, y, yd = imp_sim.simulate(2.) #Simulate 2 seconds #Plot if with_plots: P.subplot(211) P.plot(t, y[:, 0]) #, marker='o') P.xlabel('Time') P.ylabel('State') P.subplot(212) P.plot(t, yd[:, 0] * 1.e-5) #, marker='o') P.xlabel('Time') P.ylabel('State derivatives scaled with $10^{-5}$') P.suptitle(imp_mod.name) P.show() #Basic test x1 = y[:, 0] assert N.abs(x1[-1] - 1.706168035) < 1e-3 #For test purpose return imp_mod, imp_sim