Exemplo n.º 1
0
    def test_method_from_string(self):
        """Test method_from_string"""

        method = method_from_string('scipy-RK45')
        self.assertTrue(method == ScipyODE)

        method = method_from_string('zvode-adams')
        self.assertTrue(method == QiskitZVODE)
Exemplo n.º 2
0
    def test_RK4(self):
        """Run tests on RK4 fixed-step solver."""
        ode_method = method_from_string('RK4')
        options = DE_Options(max_dt=10**-3)

        # run on matrix problem
        solver = ode_method(t0=self.t0,
                            y0=self.y0,
                            rhs=self.rhs,
                            options=options)
        solver.integrate(1.)
        expected = expm(-1j * np.pi * self.X)

        # set the comparison tolerance to be somewhat lenient
        self.assertAlmostEqual(solver.y, expected, tol=10**-8)

        # test with an arbitrary problem
        def rhs(t, y):
            return np.array([t**2])

        solver = ode_method(t0=0.,
                            y0=np.array(0.),
                            rhs={'rhs': rhs},
                            options=options)

        solver.integrate(1.)
        expected = 1. / 3
        self.assertAlmostEqual(solver.y, expected, tol=10**-8)
Exemplo n.º 3
0
    def _test_variable_step_method(self, method_str):
        """Some tests for a variable step method."""

        # get method and set general options
        ode_method = method_from_string(method_str)
        options = DE_Options(method=method_str, atol=10**-10, rtol=10**-10)

        # run on matrix problem
        solver = ode_method(t0=self.t0,
                            y0=self.y0,
                            rhs=self.rhs,
                            options=options)
        solver.integrate(1.)
        expected = expm(-1j * np.pi * self.X)

        # set the comparison tolerance to be somewhat lenient
        self.assertAlmostEqual(solver.y, expected, tol=10**-8)

        # test with an arbitrary problem
        def rhs(t, y):
            return np.array([t**2])

        solver = ode_method(t0=0.,
                            y0=np.array(0.),
                            rhs={'rhs': rhs},
                            options=options)

        solver.integrate(1.)
        expected = 1. / 3
        self.assertAlmostEqual(solver.y, expected, tol=10**-9)