Example #1
0
def test_integrate_adaptive(method, use_jac):
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0.3, 0.5]
    f, j = _get_f_j(k)
    if not use_jac:
        j = None
    kwargs = dict(x0=0, xend=3, dx0=1e-10, atol=1e-8, rtol=1e-8, method=method)
    # Run twice to catch possible side-effects:
    xout, yout, info = integrate_adaptive(f, j, y0, **kwargs)
    xout, yout, info = integrate_adaptive(f, j, y0, **kwargs)
    assert info['nfev'] > 0
    if use_jac:
        assert info['njev'] > 0
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref)
Example #2
0
def test_adaptive_return_on_error():
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0.3, 0.5]
    atol, rtol = 1e-8, 1e-8
    kwargs = dict(x0=0,
                  xend=3,
                  dx0=1e-10,
                  atol=atol,
                  rtol=rtol,
                  method='rosenbrock4')
    f, j = _get_f_j(k)
    xout, yout, info = integrate_adaptive(f,
                                          j,
                                          y0,
                                          nsteps=7,
                                          return_on_error=True,
                                          **kwargs)
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref, rtol=10 * rtol, atol=10 * atol)
    assert xout.size == 8
    assert xout[-1] > 1e-6
    assert yout.shape[0] == xout.size
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success'] is False
    assert xout[-1] < kwargs['xend']  # obviously not strict
Example #3
0
def test_integrate_adaptive(method, use_jac):
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0.3, 0.5]
    f, j = _get_f_j(k)
    if not use_jac:
        j = None
    kwargs = dict(x0=0, xend=3, dx0=1e-10, atol=1e-8, rtol=1e-8, method=method)
    # Run twice to catch possible side-effects:
    xout, yout, info = integrate_adaptive(f, j, y0, **kwargs)
    xout, yout, info = integrate_adaptive(f, j, y0, **kwargs)
    assert info['success']
    assert info['atol'] == 1e-8 and info['rtol'] == 1e-8
    assert info['nfev'] > 0
    if use_jac:
        assert info['njev'] > 0
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref)
Example #4
0
def test_dx0cb():  # this test works for GSL and CVode, but it is a weak test for odeint
    k = 1e23, 3.0, 4.0
    y0 = [.7, .0, .0]
    x0, xend = 0, 5
    kwargs = dict(atol=1e-8, rtol=1e-8, method='rosenbrock4', dx0cb=lambda x, y: y[0]*1e-30)
    f, j = _get_f_j(k)
    xout, yout, info = integrate_adaptive(f, j, y0, x0, xend, **kwargs)
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref, atol=40*kwargs['atol'], rtol=40*kwargs['rtol'])
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success'] is True
    assert xout[-1] == xend
Example #5
0
def integrate_ivp(u0=1.0,
                  v0=0.0,
                  mu=1.0,
                  tend=10.0,
                  dt0=1e-8,
                  nt=0,
                  t0=0.0,
                  atol=1e-8,
                  rtol=1e-8,
                  plot=False,
                  savefig='None',
                  method='rosenbrock4',
                  dpi=100,
                  verbose=False):
    f, j = get_f_and_j(mu)
    if nt > 1:
        tout = np.linspace(t0, tend, nt)
        yout, info = integrate_predefined(f,
                                          j, [u0, v0],
                                          tout,
                                          dt0,
                                          atol,
                                          rtol,
                                          method=method,
                                          nsteps=1000)
    else:
        tout, yout, info = integrate_adaptive(f,
                                              j, [u0, v0],
                                              t0,
                                              tend,
                                              dt0,
                                              atol,
                                              rtol,
                                              method=method,
                                              nsteps=1000)
    if verbose:
        print(info)
    if plot:
        import matplotlib.pyplot as plt
        plt.plot(tout, yout)
        if savefig == 'None':
            plt.show()
        else:
            plt.savefig(savefig, dpi=dpi)
Example #6
0
def test_adaptive_autorestart():
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0.3, 0.5]
    atol, rtol = 1e-8, 1e-8
    kwargs = dict(x0=0, xend=3, dx0=1e-10, atol=atol, rtol=rtol,
                  method='rosenbrock4', nsteps=23, return_on_error=True,
                  autorestart=7+1)
    f, j = _get_f_j(k)
    xout, yout, info = integrate_adaptive(f, j, y0, **kwargs)
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref,
                       rtol=10*rtol,
                       atol=10*atol)
    assert xout[-1] > 1e-6
    assert yout.shape[0] == xout.size
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success']
    assert xout[-1] == kwargs['xend']
Example #7
0
def integrate_ivp(u0=1.0, v0=0.0, mu=1.0, tend=10.0, dt0=1e-8, nt=0,
                  t0=0.0, atol=1e-8, rtol=1e-8, plot=False, savefig='None',
                  method='rosenbrock4', dpi=100, verbose=False):
    f, j = get_f_and_j(mu)
    if nt > 1:
        tout = np.linspace(t0, tend, nt)
        yout, info = integrate_predefined(
            f, j, [u0, v0], tout, dt0, atol, rtol, method=method, nsteps=1000)
    else:
        tout, yout, info = integrate_adaptive(
            f, j, [u0, v0], t0, tend, dt0, atol, rtol, method=method, nsteps=1000)
    if verbose:
        print(info)
    if plot:
        import matplotlib.pyplot as plt
        plt.plot(tout, yout)
        if savefig == 'None':
            plt.show()
        else:
            plt.savefig(savefig, dpi=dpi)
Example #8
0
def test_dx0cb(
):  # this test works for GSL and CVode, but it is a weak test for odeint
    k = 1e23, 3.0, 4.0
    y0 = [.7, .0, .0]
    x0, xend = 0, 5
    kwargs = dict(atol=1e-8,
                  rtol=1e-8,
                  method='rosenbrock4',
                  dx0cb=lambda x, y: y[0] * 1e-30)
    f, j = _get_f_j(k)
    xout, yout, info = integrate_adaptive(f, j, y0, x0, xend, **kwargs)
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout,
                       yref,
                       atol=40 * kwargs['atol'],
                       rtol=40 * kwargs['rtol'])
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success'] is True
    assert xout[-1] == xend