Пример #1
0
def test_integrate_predefined(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
    xout = np.linspace(0, 3, 31)
    kwargs = dict(atol=1e-8, rtol=1e-8, dx0=1e-10, method=method)
    atol, rtol = 1e-8, 1e-8
    # Run twice to catch possible side-effects:
    yout, info = integrate_predefined(f, j, y0, xout, **kwargs)
    yout, info = integrate_predefined(f, j, y0, xout, **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
    if os.name == 'posix':
        assert info['time_cpu'] >= 0
        assert info['time_wall'] >= 0
Пример #2
0
def test_bad_j():
    k0, k1, k2 = decay_default_k

    def f(t, y, fout):
        fout[0] = -k0*y[0]
        fout[1] = k0*y[0] - k1*y[1]
        fout[2] = k1*y[1] - k2*y[2]

    def j(t, y, jmat_out, dfdx_out):
        y[0] = -1  # read-only! should raise ValueError
        jmat_out[0, 0] = -k0
        jmat_out[0, 1] = 0
        jmat_out[0, 2] = 0
        jmat_out[1, 0] = k0
        jmat_out[1, 1] = -k1
        jmat_out[1, 2] = 0
        jmat_out[2, 0] = 0
        jmat_out[2, 1] = k1
        jmat_out[2, 2] = -k2
        dfdx_out[0] = 0
        dfdx_out[1] = 0
        dfdx_out[2] = 0
    with pytest.raises(ValueError):
        yout, info = integrate_predefined(f, j, method='bsimp',
                                          check_callable=False,
                                          check_indexing=False,
                                          **decay_defaults)
        assert yout  # silence pyflakes
Пример #3
0
def test_bad_j():
    k0, k1, k2 = decay_default_k

    def f(t, y, fout):
        fout[0] = -k0 * y[0]
        fout[1] = k0 * y[0] - k1 * y[1]
        fout[2] = k1 * y[1] - k2 * y[2]

    def j(t, y, jmat_out, dfdx_out):
        y[0] = -1  # read-only! should raise ValueError
        jmat_out[0, 0] = -k0
        jmat_out[0, 1] = 0
        jmat_out[0, 2] = 0
        jmat_out[1, 0] = k0
        jmat_out[1, 1] = -k1
        jmat_out[1, 2] = 0
        jmat_out[2, 0] = 0
        jmat_out[2, 1] = k1
        jmat_out[2, 2] = -k2
        dfdx_out[0] = 0
        dfdx_out[1] = 0
        dfdx_out[2] = 0

    with pytest.raises(ValueError):
        yout, info = integrate_predefined(f,
                                          j,
                                          method='bsimp',
                                          check_callable=False,
                                          check_indexing=False,
                                          **decay_defaults)
        assert yout  # silence pyflakes
Пример #4
0
def test_bad_f():
    k0, k1, k2 = decay_default_k

    def f(t, y, fout):
        y[0] = -1  # read-only! should raise ValueError
        fout[0] = -k0*y[0]
        fout[1] = k0*y[0] - k1*y[1]
        fout[2] = k1*y[1] - k2*y[2]
    with pytest.raises(ValueError):
        yout, info = integrate_predefined(f, None, method='rkck',
                                          check_callable=False,
                                          check_indexing=False,
                                          **decay_defaults)
        assert yout  # silence pyflakes
Пример #5
0
def test_integrate_predefined(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
    xout = np.linspace(0, 3, 31)
    kwargs = dict(atol=1e-8, rtol=1e-8, dx0=1e-10, method=method)
    atol, rtol = 1e-8, 1e-8
    # Run twice to catch possible side-effects:
    yout, info = integrate_predefined(f, j, y0, xout, **kwargs)
    yout, info = integrate_predefined(f, j, y0, xout, **kwargs)
    yref = decay_get_Cref(k, y0, xout)
    print(yout)
    print(yref)
    assert np.allclose(yout, yref,
                       rtol=forgiveness*rtol,
                       atol=forgiveness*atol)
    assert info['nfev'] > 0
    assert info['time_cpu'] > 1e-9
    assert info['time_wall'] > 1e-9
    if method in requires_jac:
        assert info['njev'] > 0
Пример #6
0
def test_predefined_return_on_error():
    k = k0, k1, k2 = 2.0, 300.0, 4.0
    y0 = [0.7, 0.0, 0.2]
    atol, rtol = 1e-12, 1e-12
    kwargs = dict(dx0=1e-11, atol=atol, rtol=rtol, method='bsimp',
                  return_on_error=True, nsteps=10)
    f, j = _get_f_j(k)
    xout = np.logspace(-10, 1, 5)
    yout, info = integrate_predefined(f, j, y0, xout, **kwargs)
    yref = decay_get_Cref(k, y0, xout - xout[0])
    assert np.allclose(yout[:info['nreached'], :], yref[:info['nreached'], :],
                       rtol=10*rtol, atol=10*atol)
    assert 2 < info['nreached'] < 5  # not strict
    assert yout.shape[0] == xout.size
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success'] is False
Пример #7
0
def test_bad_f():
    k0, k1, k2 = decay_default_k

    def f(t, y, fout):
        y[0] = -1  # read-only! should raise ValueError
        fout[0] = -k0 * y[0]
        fout[1] = k0 * y[0] - k1 * y[1]
        fout[2] = k1 * y[1] - k2 * y[2]

    with pytest.raises(ValueError):
        yout, info = integrate_predefined(f,
                                          None,
                                          method='rkck',
                                          check_callable=False,
                                          check_indexing=False,
                                          **decay_defaults)
        assert yout  # silence pyflakes
Пример #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)
Пример #9
0
def test_predefined_autorestart():
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0.3, 0.5]
    atol, rtol = 1e-8, 1e-8
    x0, xend = 0, 3
    kwargs = dict(dx0=1e-10, atol=atol, rtol=rtol,
                  method='bsimp', nsteps=62,
                  autorestart=10)
    f, j = _get_f_j(k)
    xout = np.linspace(x0, xend)
    yout, info = integrate_predefined(f, j, y0, xout, **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] == xend
Пример #10
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)
Пример #11
0
def test_predefined_autorestart():
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0.3, 0.5]
    atol, rtol = 1e-8, 1e-8
    x0, xend = 0, 3
    kwargs = dict(dx0=1e-10,
                  atol=atol,
                  rtol=rtol,
                  method='bsimp',
                  nsteps=62,
                  autorestart=10)
    f, j = _get_f_j(k)
    xout = np.linspace(x0, xend)
    yout, info = integrate_predefined(f, j, y0, xout, **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] == xend
Пример #12
0
def test_predefined_return_on_error():
    k = k0, k1, k2 = 2.0, 300.0, 4.0
    y0 = [0.7, 0.0, 0.2]
    atol, rtol = 1e-12, 1e-12
    kwargs = dict(dx0=1e-11,
                  atol=atol,
                  rtol=rtol,
                  method='bsimp',
                  return_on_error=True,
                  nsteps=10)
    f, j = _get_f_j(k)
    xout = np.logspace(-10, 1, 5)
    yout, info = integrate_predefined(f, j, y0, xout, **kwargs)
    yref = decay_get_Cref(k, y0, xout - xout[0])
    assert np.allclose(yout[:info['nreached'], :],
                       yref[:info['nreached'], :],
                       rtol=10 * rtol,
                       atol=10 * atol)
    assert 2 < info['nreached'] < 5  # not strict
    assert yout.shape[0] == xout.size
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success'] is False