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)
def test_method(): wt_linear = V80() wt_pchip = V80(method='pchip') wt_spline = V80(method='spline') ws_lst = np.arange(3, 25, .001) for wt in [wt_linear, wt_pchip, wt_spline]: wt.enable_autograd() ws_pts = [6.99, 7.01] with use_autograd_in(): dpdu_linear_pts = autograd(wt_linear.power)(np.array(ws_pts)) dpdu_pchip_pts = autograd(wt_pchip.power)(np.array(ws_pts)) dpdu_spline_pts = autograd(wt_spline.power)(np.array(ws_pts)) if 0: wt_dp_label_lst = [(wt_linear, dpdu_linear_pts, 'linear'), (wt_pchip, dpdu_pchip_pts, 'pchip'), (wt_spline, dpdu_spline_pts, 'spline')] for wt, dpdu_pts, label in wt_dp_label_lst: c = plt.plot(ws_lst, wt.power(ws_lst), label=label)[0].get_color() gradients.color_dict[label] = c for ws, dpdu in zip(ws_pts, dpdu_pts): plot_gradients(wt.power(ws), dpdu, ws, label) plt.legend() plt.figure() for wt, dpdu_pts, label in wt_dp_label_lst: plt.plot(ws_lst, wt_linear.power(ws_lst) - wt.power(ws_lst), label=label) plt.legend() plt.ylabel('Power difference wrt. linear') plt.figure() for wt, dpdu_pts, label in wt_dp_label_lst: plt.plot(np.diff(np.diff(wt.power(ws_lst))), label=label) plt.ylabel('Change of gradient') plt.legend() plt.show() # mean and max error for wt, mean_tol, absmean_tol, max_tol in [(wt_pchip, 213, 2323, 15632), (wt_spline, 1, 1, 1380)]: assert np.abs((wt_linear.power(ws_lst) - wt.power(ws_lst)).mean()) < mean_tol assert np.abs((wt_linear.power(ws_lst) - wt.power(ws_lst)).mean()) < absmean_tol assert np.abs((wt_linear.power(ws_lst) - wt.power(ws_lst)).max()) < max_tol for wt, diff_grad_max, dpdu_pts, ref_dpdu_pts in [(wt_linear, 64, dpdu_linear_pts, [178000.00007264, 236000.00003353]), (wt_pchip, 0.2, dpdu_pchip_pts, [ 202520.16516056, 203694.66294614]), (wt_spline, 0.8, dpdu_spline_pts, [205555.17794162, 211859.45965873])]: assert np.diff(np.diff(wt.power(ws_lst))).max() < diff_grad_max npt.assert_array_almost_equal(dpdu_pts, ref_dpdu_pts)
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)
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()
def test_set_gradients(): wt = IEA37_WindTurbines() def dpctdu(ws, run_only): if run_only == 0: return np.where((ws > 4) & (ws <= 9.8), 100000 * ws, # not the right gradient, but similar to the reference 0) else: return np.full(ws.shape, 0) wt.powerCtFunction.set_gradient_funcs(dpctdu) with use_autograd_in([WindTurbines, iea37_reader, power_ct_functions, wind_turbines_deprecated]): ws_lst = np.arange(3, 25, .1) plt.plot(ws_lst, wt.power(ws_lst)) ws_pts = np.array([3., 6., 9., 12.]) dpdu_lst = autograd(wt.power)(ws_pts) if 0: 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), 100000 * ws_pts, 0) npt.assert_array_almost_equal(dpdu_lst, dpdu_ref)
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)
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)
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)
def test_gradients(): wt = IEA37_WindTurbines() with use_autograd_in([WindTurbines, iea37_reader]): ws_lst = np.arange(3, 25, .1) plt.plot(ws_lst, wt.power(ws_lst)) ws_pts = np.array([3., 6., 9., 12.]) dpdu_lst = np.diag(autograd(wt.power)(ws_pts)) if 0: 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)
def test_set_gradients(): wt = IEA37_WindTurbines() wt.set_gradient_funcs(lambda ws: np.where((ws > 4) & (ws <= 9.8), 100000 * ws, # not the right gradient, but similar to the reference 0), lambda ws: 0) with use_autograd_in([WindTurbines, iea37_reader]): ws_lst = np.arange(3, 25, .1) plt.plot(ws_lst, wt.power(ws_lst)) ws_pts = np.array([3., 6., 9., 12.]) dpdu_lst = np.diag(autograd(wt.power)(ws_pts)) if 0: 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), 100000 * ws_pts, 0) npt.assert_array_almost_equal(dpdu_lst, dpdu_ref)
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)
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)