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_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 aep(x, y, h=None, type=0, wd=None, ws=None, yaw_ilk=None): # @ReservedAssignment if gradient_method == autograd: with use_autograd_in(): return self.aep(x, y, h, type, wd, ws, yaw_ilk) else: return self.aep(x, y, h, type, wd, ws, yaw_ilk)
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 aep(x, y, h, type, wd, ws, yaw_ilk): # @ReservedAssignment if gradient_method == autograd: with use_autograd_in(): return self.aep( x, y, h, type, wd, ws, yaw_ilk, normalize_probabilities=normalize_probabilities, with_wake_loss=with_wake_loss) else: return self.aep( x, y, h, type, wd, ws, yaw_ilk, normalize_probabilities=normalize_probabilities, with_wake_loss=with_wake_loss)
def test_use_autograd_in(obj): assert wind_turbines.np == np with use_autograd_in([obj]): assert wind_turbines.np == anp assert wind_turbines.np == np
ct=v80_upct[2]), PowerCtTabular(ws=v80_upct[0], power=v80_upct[1] * 1.1, power_unit='w', ct=v80_upct[2] + .1) ])), [73260., 195800., 379499.9998, 101199.9999 ], [0.818, 0.001, -0.014, -0.3])]) @pytest.mark.parametrize('grad_method', [autograd, cs, fd]) def test_gradients(case, wt, dpdu_ref, dctdu_ref, grad_method): ws_pts = np.array([3.1, 6., 9., 12.]) if isinstance(dpdu_ref, FunctionType): dpdu_ref, dctdu_ref = dpdu_ref(ws_pts), dctdu_ref(ws_pts) with use_autograd_in([ WindTurbines, iea37_reader, power_ct_functions, wind_turbines_deprecated ]): if grad_method == autograd: wt.enable_autograd() ws_lst = np.arange(2, 25, .1) kwargs = {k: 1 for k in wt.function_inputs[0]} dpdu_lst = grad_method(wt.power)(ws_pts, **kwargs) dctdu_lst = grad_method(wt.ct)(ws_pts, **kwargs) if 0: gradients.color_dict = {'power': 'b', 'ct': 'r'} plt.plot(ws_lst, wt.power(ws_lst, **kwargs), label='power') c = plt.plot([], label='ct')[0].get_color() for dpdu, ws in zip(dpdu_lst, ws_pts): plot_gradients(wt.power(ws, **kwargs), dpdu, ws, "power", 1) ax = plt.twinx()