def test_plot_gradients(): x = np.arange(-3, 4, .1) plt.plot(x, x**2) plot_gradients(1.5**2, 3, 1.5, "test", 1) if 0: plt.show() plt.close()
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_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_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_dAEP_2wt(): site = Hornsrev1Site() iea37_site = IEA37Site(16) wt = IEA37_WindTurbines() wfm = IEA37SimpleBastankhahGaussian(site, wt) x, y = iea37_site.initial_position[np.array([0, 2, 5, 8, 14])].T # plot 2 wt case x, y = np.array([[0, 130 * 4], [0, 0]], dtype=np.float) x_lst = np.array([0., 1.]) * np.arange(1, 600, 10)[:, na] kwargs = {'ws': [10], 'wd': [270]} _, (ax1, ax2) = plt.subplots(1, 2, sharey=False) ax1.plot(x_lst[:, 1], [wfm.aep(x_, y, **kwargs) for x_ in x_lst]) ax1.set_xlabel('Downwind distance [m]') ax1.set_ylabel('AEP [GWh]') x_ = x_lst[20] ax1.set_title("Center line") for grad in [fd, cs, autograd]: dAEPdx = wfm.dAEPdn(0, grad)(x_, y, **kwargs)[1] npt.assert_almost_equal(dAEPdx / 360, 3.976975605364392e-06, (10, 5)[grad == fd]) plot_gradients(wfm.aep(x_, y, **kwargs), dAEPdx, x_[1], grad.__name__, step=100, ax=ax1) y_lst = np.array([0, 1.]) * np.arange(-100, 100, 5)[:, na] ax2.plot(y_lst[:, 1], [wfm.aep(x, y_, **kwargs) for y_ in y_lst]) ax2.set_xlabel('Crosswind distance [m]') ax2.set_ylabel('AEP [GWh]') y_ = y_lst[25] ax2.set_title("%d m downstream" % x[1]) for grad in [fd, cs, autograd]: dAEPdy = wfm.dAEPdn(1, grad)(x, y_, **kwargs)[1] plot_gradients(wfm.aep(x, y_, **kwargs), dAEPdy, y_[1], grad.__name__, step=50, ax=ax2) npt.assert_almost_equal(dAEPdy / 360, 3.794435973860448e-05, (10, 5)[grad == fd]) if 0: plt.legend() plt.show() plt.close()
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)
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() ax.plot(ws_lst, wt.ct(ws_lst, **kwargs), color=c) for dctdu, ws in zip(dctdu_lst, ws_pts): plot_gradients(wt.ct(ws, **kwargs), dctdu, ws, "ct", 1, ax=ax) plt.title(case) plt.show() npt.assert_array_almost_equal(dpdu_lst, dpdu_ref, (0, 4)[grad_method == autograd]) npt.assert_array_almost_equal(dctdu_lst, dctdu_ref, (4, 6)[grad_method == autograd]) if __name__ == '__main__': x = np.linspace(0, 2 * np.pi, 100)
def test_dAEP_2wt(): site = Hornsrev1Site() iea37_site = IEA37Site(16) wsp_cut_in = 4 wsp_cut_out = 25 wsp_rated = 9.8 power_rated = 3350000 constant_ct = 8 / 9 def ct(wsp): wsp = np.asarray(wsp) ct = np.zeros_like(wsp, dtype=float) ct[(wsp >= wsp_cut_in) & (wsp <= wsp_cut_out)] = constant_ct return ct def power(wsp): wsp = np.asarray(wsp) power = np.where((wsp > wsp_cut_in) & (wsp <= wsp_cut_out), np.minimum( power_rated * ((wsp - wsp_cut_in) / (wsp_rated - wsp_cut_in))**3, power_rated), 0) return power def dpower(wsp): return np.where((wsp > wsp_cut_in) & (wsp <= wsp_rated), 3 * power_rated * (wsp - wsp_cut_in)**2 / (wsp_rated - wsp_cut_in)**3, 0) def dct(wsp): return wsp * 0 # constant ct wt = DeprecatedOneTypeWindTurbines(name='test', diameter=130, hub_height=110, ct_func=ct, power_func=power, power_unit='w') wt.set_gradient_funcs(dpower, dct) wfm = IEA37SimpleBastankhahGaussian(site, wt) x, y = iea37_site.initial_position[np.array([0, 2, 5, 8, 14])].T # plot 2 wt case x, y = np.array([[0, 130 * 4], [0, 0]], dtype=float) x_lst = np.array([0., 1.]) * np.arange(1, 600, 10)[:, na] kwargs = {'ws': [10], 'wd': [270]} _, (ax1, ax2) = plt.subplots(1, 2, sharey=False) ax1.plot(x_lst[:, 1], [wfm.aep(x_, y, **kwargs) for x_ in x_lst]) ax1.set_xlabel('Downwind distance [m]') ax1.set_ylabel('AEP [GWh]') x_ = x_lst[20] ax1.set_title("Center line") for grad in [fd, cs, autograd]: dAEPdx = wfm.dAEPdn(0, grad)(x_, y, **kwargs)[1] npt.assert_almost_equal(dAEPdx / 360, 3.976975605364392e-06, (10, 5)[grad == fd]) plot_gradients(wfm.aep(x_, y, **kwargs), dAEPdx, x_[1], grad.__name__, step=100, ax=ax1) y_lst = np.array([0, 1.]) * np.arange(-100, 100, 5)[:, na] ax2.plot(y_lst[:, 1], [wfm.aep(x, y_, **kwargs) for y_ in y_lst]) ax2.set_xlabel('Crosswind distance [m]') ax2.set_ylabel('AEP [GWh]') y_ = y_lst[25] ax2.set_title("%d m downstream" % x[1]) for grad in [fd, cs, autograd]: dAEPdy = wfm.dAEPdn(1, grad)(x, y_, **kwargs)[1] plot_gradients(wfm.aep(x, y_, **kwargs), dAEPdy, y_[1], grad.__name__, step=50, ax=ax2) npt.assert_almost_equal(dAEPdy / 360, 3.794435973860448e-05, (10, 5)[grad == fd]) if 0: plt.legend() plt.show() plt.close('all')