Пример #1
0
def test_integrate_adaptive(method, forgiveness):
    use_jac = method in requires_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
    atol, rtol = 1e-8, 1e-8
    kwargs = dict(x0=0, xend=3, dx0=1e-10, atol=atol, rtol=rtol,
                  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)
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref,
                       rtol=forgiveness*rtol,
                       atol=forgiveness*atol)
    assert info['nfev'] > 0
    if method in requires_jac:
        assert info['njev'] > 0

    with pytest.raises(RuntimeError) as excinfo:
        integrate_adaptive(f, j, y0, nsteps=7, **kwargs)
    assert 'steps' in str(excinfo.value).lower()
    assert '7' in str(excinfo.value).lower()
Пример #2
0
def test_integrate_adaptive(method, forgiveness):
    use_jac = method in requires_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
    atol, rtol = 1e-8, 1e-8
    kwargs = dict(x0=0, xend=3, dx0=1e-10, atol=atol, rtol=rtol, 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)
    yref = decay_get_Cref(k, y0, xout)
    assert info['success']
    assert info['atol'] == atol and info['rtol'] == rtol
    assert np.allclose(yout,
                       yref,
                       rtol=forgiveness * rtol,
                       atol=forgiveness * atol)
    assert info['nfev'] > 0
    if method in requires_jac:
        assert info['njev'] > 0

    with pytest.raises(RuntimeError) as excinfo:
        integrate_adaptive(f, j, y0, nsteps=7, **kwargs)
    assert 'steps' in str(excinfo.value).lower()
    assert '7' in str(excinfo.value).lower()
Пример #3
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='bsimp')
    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
Пример #4
0
def test_dx0cb():
    k = 1e23, 3.0, 4.0
    y0 = [.7, .0, .0]
    x0, xend = 0, 5
    kwargs = dict(atol=1e-8, rtol=1e-8, method='bsimp', 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
Пример #5
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='bsimp')
    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
Пример #6
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='bsimp',
                  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,
                                          check_indexing=False,
                                          method=method)
    else:
        tout, yout, info = integrate_adaptive(
            f,
            j, [u0, v0],
            t0,
            tend,
            dt0,
            atol,
            rtol,
            check_indexing=False,
            method=method)  # dfdt[:] also for len == 1
    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)
Пример #7
0
def test_dx0cb():
    k = 1e23, 3.0, 4.0
    y0 = [.7, .0, .0]
    x0, xend = 0, 5
    kwargs = dict(atol=1e-8,
                  rtol=1e-8,
                  method='bsimp',
                  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
Пример #8
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='bsimp', 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,
            check_indexing=False, method=method)
    else:
        tout, yout, info = integrate_adaptive(
            f, j, [u0, v0], t0, tend, dt0, atol, rtol,
            check_indexing=False, method=method)  # dfdt[:] also for len == 1
    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)