Exemplo n.º 1
0
def test_integrate_predefined(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
    xout = np.linspace(0, 3)
    dx0 = 1e-10
    # Run twice to catch possible side-effects:
    yout, info = integrate_predefined(f,
                                      j,
                                      y0,
                                      xout,
                                      1e-9,
                                      1e-9,
                                      dx0,
                                      method=method)
    yout, info = integrate_predefined(f,
                                      j,
                                      y0,
                                      xout,
                                      1e-9,
                                      1e-9,
                                      dx0,
                                      method=method)
    assert info['success']
    assert info['atol'] == 1e-9 and info['rtol'] == 1e-9
    assert info['nfev'] > 0
    if use_jac:
        assert info['njev'] > 0
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref)
    if os.name == 'posix':
        assert info['time_wall'] >= 0
        assert info['time_cpu'] >= 0
Exemplo n.º 2
0
def test_integrate_predefined(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
    xout = np.linspace(0, 3)
    dx0 = 1e-10
    # Run twice to catch possible side-effects:
    yout, info = integrate_predefined(f, j, y0, xout, 1e-9, 1e-9, dx0,
                                      method=method)
    yout, info = integrate_predefined(f, j, y0, xout, 1e-9, 1e-9, dx0,
                                      method=method)
    assert info['success']
    assert info['nfev'] > 0
    if use_jac:
        assert info['njev'] > 0
    yref = decay_get_Cref(k, y0, xout)
    assert np.allclose(yout, yref)
    assert 1e-9 < info['time_wall'] < 1.0  # Takes a few ms on a 2012 desktop computer
    assert 1e-9 < info['time_cpu'] < 1.0  # Takes a few ms on a 2012 desktop computer
Exemplo n.º 3
0
def test_predefined_return_on_error():
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0., 0.]
    atol, rtol = 1e-8, 1e-8
    kwargs = dict(dx0=1e-10, atol=atol, rtol=rtol,
                  method='rosenbrock4', return_on_error=True, nsteps=12)
    f, j = _get_f_j(k)
    xout = np.logspace(-3, 1)
    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 10 < info['nreached'] < 40
    assert yout.shape[0] == xout.size
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success'] is False
Exemplo n.º 4
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)
Exemplo n.º 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)
Exemplo n.º 6
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='rosenbrock4', 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
Exemplo n.º 7
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='rosenbrock4',
                  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
Exemplo n.º 8
0
def test_predefined_return_on_error():
    k = k0, k1, k2 = 2.0, 3.0, 4.0
    y0 = [0.7, 0., 0.]
    atol, rtol = 1e-8, 1e-8
    kwargs = dict(dx0=1e-10,
                  atol=atol,
                  rtol=rtol,
                  method='rosenbrock4',
                  return_on_error=True,
                  nsteps=12)
    f, j = _get_f_j(k)
    xout = np.logspace(-3, 1)
    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 10 < info['nreached'] < 40
    assert yout.shape[0] == xout.size
    assert info['nfev'] > 0
    assert info['njev'] > 0
    assert info['success'] is False