Example #1
0
def test_multivector2vector_independent():
    def f(x, y):
        return x**2 + 2 * y**3 + 1

    def dfdx(x, y):
        return 2 * x

    def dfdy(x, y):
        return 6 * y**2

    x = np.array([2, 3, 4])
    y = np.array([1, 2, 3])
    ref_x = [4, 6, 8]
    ref_y = [6, 24, 54]
    npt.assert_array_almost_equal(fd(f, False)(x, y), ref_x, 5)
    npt.assert_array_almost_equal(fd(f, False, 1)(x, y), ref_y, 4)

    npt.assert_array_almost_equal_nulp(cs(f, False)(x, y), ref_x)
    npt.assert_array_almost_equal_nulp(cs(f, False, 1)(x, y), ref_y)

    npt.assert_array_equal(autograd(f, False)(x, y), ref_x)
    npt.assert_array_equal(autograd(f, False, 1)(x, y), ref_y)

    pf = primitive(f)
    defvjp(pf, lambda ans, x, y: lambda g: g * dfdx(x, y),
           lambda ans, x, y: lambda g: g * dfdy(x, y))
    npt.assert_array_equal(autograd(pf, False)(x, y), ref_x)
    npt.assert_array_equal(autograd(pf, False, 1)(x, y), ref_y)
Example #2
0
def test_spline():
    wt_tab = V80()
    wt_spline = V80()
    wt_spline.spline_ct_power(err_tol_factor=1e-2)
    ws_lst = np.arange(3, 25, .001)

    # mean and max error
    assert (wt_tab.power(ws_lst) - wt_spline.power(ws_lst)).mean() < 1
    assert ((wt_tab.power(ws_lst) - wt_spline.power(ws_lst)).max()) < 1400

    # max change of gradient 80 times lower
    assert np.diff(np.diff(wt_spline.power(ws_lst))).max() * 80 < np.diff(np.diff(wt_tab.power(ws_lst))).max()

    ws_pts = [6.99, 7.01]
    dpdu_tab_pts = np.diag(fd(wt_tab.power)(np.array(ws_pts)))
    with use_autograd_in():
        dpdu_spline_pts = np.diag(autograd(wt_spline.power)(np.array(ws_pts)))
    npt.assert_array_almost_equal(dpdu_spline_pts, [205555.17794162, 211859.45965873])

    if 0:
        plt.plot(ws_lst, wt_tab.power(ws_lst))
        plt.plot(ws_lst, wt_spline.power(ws_lst))

        for wt, dpdu_pts, label in [(wt_tab, dpdu_tab_pts, 'V80 tabular'),
                                    (wt_spline, dpdu_spline_pts, 'V80 spline')]:
            for ws, dpdu in zip(ws_pts, dpdu_pts):
                plot_gradients(wt.power(ws), dpdu, ws, label, 1)

        ax = plt.gca().twinx()
        ax.plot(ws_lst, wt.power(ws_lst) - wt_spline.power(ws_lst))
        plt.figure()
        plt.plot(np.diff(np.diff(wt_tab.power(ws_lst))))
        plt.plot(np.diff(np.diff(wt_spline.power(ws_lst))))
        plt.show()
Example #3
0
def test_scalar2scalar():
    def f(x):
        return x**2 + 1

    x = np.array([3])
    npt.assert_equal(cs(f)(x), 6)
    npt.assert_almost_equal(fd(f)(x), 6, 5)
    npt.assert_equal(autograd(f)(x), 6)
    pf = primitive(f)
    defvjp(pf, lambda ans, x: lambda g: g * 2 * x)
    npt.assert_array_equal(autograd(pf, False)(x), 6)
Example #4
0
def test_vector2vector_dependent():
    def f(x):
        return x**2 + x[::-1]

    def df(x):
        return np.diag(2 * x) + np.diag(np.ones(3))[::-1]

    x = np.array([2., 3, 4])
    ref = [[4., 0., 1.], [0., 7., 0.], [1., 0., 8.]]
    npt.assert_array_almost_equal(fd(f, True)(x), ref, 5)
    npt.assert_array_almost_equal_nulp(cs(f, True)(x), ref)
    npt.assert_array_equal(autograd(f, True)(x), ref)

    pf = primitive(f)
    defvjp(pf, lambda ans, x: lambda g: np.dot(g, df(x)))
    npt.assert_array_equal(autograd(pf, True)(x), ref)
Example #5
0
def test_vector2vector_independent():
    def f(x):
        return x**2 + 1

    def df(x):
        return 2 * x

    x = np.array([2, 3, 4])
    ref = [4, 6, 8]
    npt.assert_array_almost_equal(fd(f, False)(x), ref, 5)
    npt.assert_array_equal(cs(f, False)(x), ref)
    npt.assert_array_equal(autograd(f, False)(x), ref)

    pf = primitive(f)
    defvjp(pf, lambda ans, x: lambda g: g * df(x))
    npt.assert_array_equal(autograd(pf, False)(x), ref)
Example #6
0
def test_scalar2multi_scalar():
    def fxy(x):
        return x**2 + 1, 2 * x + 1

    def f(x):
        fx, fy = fxy(x)
        return fx + fy

    x = 3.
    ref = 8
    npt.assert_equal(cs(f)(x), ref)
    npt.assert_almost_equal(fd(f)(x), ref, 5)
    npt.assert_equal(autograd(f)(x), ref)

    pf = primitive(f)
    defvjp(pf, lambda ans, x: lambda g: g * (2 * x + 2))
    npt.assert_array_equal(autograd(pf, False)(x), ref)

    pf = primitive(fxy)
    defvjp(pf, lambda ans, x: lambda g: (g[0] * 2 * x, g[1] * 2))
    npt.assert_array_equal(autograd(f, False)(x), ref)
Example #7
0
def test_gradients():
    wt = IEA37_WindTurbines()
    wt.enable_autograd()
    ws_lst = np.arange(3, 25, .1)

    ws_pts = np.array([3., 6., 9., 12.])
    dpdu_lst = autograd(wt.power)(ws_pts)
    if 0:
        plt.plot(ws_lst, wt.power(ws_lst))
        for dpdu, ws in zip(dpdu_lst, ws_pts):
            plot_gradients(wt.power(ws), dpdu, ws, "", 1)

        plt.show()
    dpdu_ref = np.where((ws_pts > 4) & (ws_pts <= 9.8),
                        3 * 3350000 * (ws_pts - 4)**2 / (9.8 - 4)**3, 0)

    npt.assert_array_almost_equal(dpdu_lst, dpdu_ref)

    fd_dpdu_lst = fd(wt.power)(ws_pts)
    npt.assert_array_almost_equal(fd_dpdu_lst, dpdu_ref, 0)

    cs_dpdu_lst = cs(wt.power)(ws_pts)
    npt.assert_array_almost_equal(cs_dpdu_lst, dpdu_ref)
Example #8
0
 def power_ct_grad_func(ws, run_only):
     # fd is fine for linear interpolation
     return fd(lambda ws, run_only=run_only, self=self: self.
               np_interp(ws, run_only))(ws)
Example #9
0
def test_vector2multi_vector():
    def fxy(x):
        return x**2 + 1, 2 * x + 1

    def f0(x):
        return fxy(x)[0]

    def fsum(x):
        fx, fy = fxy(x)
        return fx + fy

    x = np.array([1., 2, 3])
    ref0 = [2, 4, 6]
    refsum = [4, 6, 8]
    npt.assert_equal(cs(f0)(x), ref0)
    npt.assert_almost_equal(fd(f0)(x), ref0, 5)
    npt.assert_equal(autograd(f0)(x), ref0)
    pf0 = primitive(f0)
    defvjp(pf0, lambda ans, x: lambda g: g * (2 * x))
    npt.assert_array_equal(autograd(pf0, False)(x), ref0)

    npt.assert_equal(cs(fsum)(x), refsum)
    npt.assert_almost_equal(fd(fsum)(x), refsum, 5)
    npt.assert_equal(autograd(fsum)(x), refsum)
    pfsum = primitive(fsum)
    defvjp(pfsum, lambda ans, x: lambda g: g * (2 * x + 2))
    npt.assert_array_equal(autograd(pfsum, False)(x), refsum)

    pfxy = primitive(fxy)

    def dfxy(x):
        return 2 * x, np.full(x.shape, 2)

    def gsum(x):
        fx, fy = pfxy(x)
        return fx + fy

    def g0(x):
        return pfxy(x)[0]

    pgsum = primitive(gsum)
    pg0 = primitive(g0)
    defvjp(pgsum, lambda ans, x: lambda g: g * np.sum(dfxy(x), 0))
    defvjp(pg0, lambda ans, x: lambda g: g * dfxy(x)[0])

    npt.assert_array_equal(autograd(pgsum, False)(x), refsum)
    npt.assert_array_equal(autograd(pg0, False)(x), ref0)

    defvjp(pfxy, lambda ans, x: lambda g: dfxy(x)[0])

    def h0(x):
        return pfxy(x)[0]

    npt.assert_array_equal(autograd(h0, False)(x), ref0)

    defvjp(pfxy, lambda ans, x: lambda g: np.sum(g * np.asarray(dfxy(x)), 0))

    def hsum(x):
        fx, fy = pfxy(x)
        return fx + fy

    npt.assert_array_equal(autograd(hsum, False)(x), refsum)