def test_local_wind(site): x_i = y_i = np.arange(5) wdir_lst = np.arange(0, 360, 90) wsp_lst = np.arange(3, 6) lw = site.local_wind(x_i=x_i, y_i=y_i, wd=wdir_lst, ws=wsp_lst) npt.assert_array_equal(lw.WS_ilk.shape, (5, 4, 3)) lw = site.local_wind(x_i=x_i, y_i=y_i) npt.assert_array_equal(lw.WS_ilk.shape, (5, 360, 23)) # check probability local_wind()[-1] npt.assert_equal(site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[10], wd_bin_size=1).P_ilk, site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[10], wd_bin_size=2).P_ilk / 2) npt.assert_almost_equal(site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[9, 10, 11]).P_ilk.sum((1, 2)), site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[10], ws_bins=3).P_ilk[:, 0, 0], 5) z = np.arange(1, 100) zero = [0] * len(z) ws = site.local_wind(x_i=zero, y_i=zero, h_i=z, wd=[0], ws=[10]).WS_ilk[:, 0, 0] site2 = UniformWeibullSite(f, A, k, ti, shear=PowerShear(70, alpha=np.zeros_like(f) + .3)) ws70 = site2.local_wind(x_i=zero, y_i=zero, h_i=z, wd=[0], ws=[10]).WS_ilk[:, 0, 0] if 0: import matplotlib.pyplot as plt plt.plot(ws, z) plt.plot(ws70, z) plt.show() npt.assert_array_equal(10 * (z / 50)**.3, ws) npt.assert_array_equal(10 * (z / 70)**.3, ws70)
def test_interpolation(): wts = HornsrevV80() path = tfp + 'fuga/2MW/Z0=0.00408599Zi=00400Zeta0=0.00E+00/' site = UniformSite([1, 0, 0, 0], ti=0.075) plot = 0 if plot: ax1 = plt.gca() ax2 = plt.twinx() for wdm, n_d_values in ( (FugaDeficit(path, method='linear'), 4), (FugaDeficit(path, method='spline'), 20), (FugaYawDeficit(path, method='linear'), 4), (FugaYawDeficit(path, method='spline'), 20), ): wfm = PropagateDownwind(site, wts, wdm) sim_res = wfm(x=[0], y=[0], wd=[270], ws=[10], yaw=[[[10]]]) fm = sim_res.flow_map(XYGrid(x=[200], y=np.arange(-10, 11))) fm = sim_res.flow_map( XYGrid(x=np.arange(-100, 800, 10), y=np.arange(-10, 11))) # linear has 4 line segments with same gradient, while spline has 20 different gradient values npt.assert_equal( len(np.unique(np.round(np.diff(fm.WS_eff.sel(x=500).squeeze()), 6))), n_d_values) if plot: ax1.plot(fm.y, fm.WS_eff.sel(x=500).squeeze()) ax2.plot(fm.y[:-1], np.diff(fm.WS_eff.sel(x=500).squeeze()), '--') if plot: plt.show() plt.close('all')
def test_with_all_deficit_models(WFM, deficitModel): site = IEA37Site(16) windTurbines = IEA37_WindTurbines() wfm = WFM(site, windTurbines, wake_deficitModel=deficitModel, rotorAvgModel=RotorCenter(), superpositionModel=LinearSum(), deflectionModel=None, turbulenceModel=STF2017TurbulenceModel()) wfm2 = WFM(site, windTurbines, wake_deficitModel=deficitModel, rotorAvgModel=EqGridRotorAvg(1), superpositionModel=LinearSum(), deflectionModel=None, turbulenceModel=STF2017TurbulenceModel()) kwargs = { 'x': [0, 0, 500, 500], 'y': [0, 500, 0, 500], 'wd': [0], 'ws': [8] } npt.assert_equal(wfm.aep(**kwargs), wfm2.aep(**kwargs))
def test_wd_independent_site(): ti = 0.1 ds = xr.Dataset( data_vars={ 'WS': 10, 'Sector_frequency': 1, 'Weibull_A': 4, 'Weibull_k': 2, 'TI': ti}, coords={}) site = XRSite(ds, shear=None) npt.assert_equal(site.ds.sector_width, 360)
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_aep(): site = UniformSite([1], ti=0) windTurbines = IEA37_WindTurbines() wfm = NOJ(site, windTurbines) sim_res = wfm([0], [0], wd=270) npt.assert_almost_equal(sim_res.aep().sum(), 3.35 * 24 * 365 / 1000) npt.assert_almost_equal(sim_res.aep(normalize_probabilities=True).sum(), 3.35 * 24 * 365 / 1000) npt.assert_equal(sim_res.aep().data.sum(), wfm.aep([0], [0], wd=270)) npt.assert_almost_equal(sim_res.aep(normalize_probabilities=True).sum(), wfm.aep([0], [0], wd=270, normalize_probabilities=True)) npt.assert_almost_equal(sim_res.aep(with_wake_loss=False).sum(), wfm.aep([0], [0], wd=270, with_wake_loss=False))
def test_read_iea_windturbine(): wt_id, hubheight, diameter, ct, power, dct, dpower = read_iea37_windturbine( iea37_path + 'iea37-335mw.yaml') assert wt_id == "3.35MW" assert hubheight == 110 assert diameter == 130 u = np.arange(30) p_r = 3350000 npt.assert_array_almost_equal([0, 1 / 5.8**3 * p_r, p_r, p_r, 0], power([4, 5, 9.8, 25, 25.1])) ct_ = 4 * 1 / 3 * (1 - 1 / 3) npt.assert_array_almost_equal([0, ct_, ct_, 0], ct([3.9, 4, 25, 25.1])) npt.assert_almost_equal(dpower(7), cs(power)(7)) npt.assert_equal(dct(7), 0) if 0: import matplotlib.pyplot as plt plt.plot(u, power(u) / 1e6) plt.plot(u, ct(u)) plt.show()
def test_save_load(): site = UniformSite([1], ti=0) windTurbines = IEA37_WindTurbines() wfm = NOJ(site, windTurbines) sim_res1 = wfm([0], [0], wd=270) sim_res1.save(tfp + "tmp.nc") sim_res2 = SimulationResult.load(tfp + 'tmp.nc', wfm) for sim_res in [sim_res1, sim_res2]: npt.assert_almost_equal(sim_res.aep().sum(), 3.35 * 24 * 365 / 1000) npt.assert_almost_equal(sim_res.aep(normalize_probabilities=True).sum(), 3.35 * 24 * 365 / 1000) npt.assert_equal(sim_res.aep().data.sum(), wfm.aep([0], [0], wd=270)) npt.assert_almost_equal(sim_res.aep(normalize_probabilities=True).sum(), wfm.aep([0], [0], wd=270, normalize_probabilities=True)) npt.assert_almost_equal(sim_res.aep(with_wake_loss=False).sum(), wfm.aep([0], [0], wd=270, with_wake_loss=False))
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_local_wind(site): x_i = y_i = np.arange(5) wdir_lst = np.arange(0, 360, 90) wsp_lst = np.arange(3, 6) WD_ilk, WS_ilk, TI_ilk, P_lk = site.local_wind(x_i=x_i, y_i=y_i, wd=wdir_lst, ws=wsp_lst) npt.assert_array_equal(WS_ilk.shape, (5, 4, 3)) WD_ilk, WS_ilk, TI_ilk, P_lk = site.local_wind(x_i=x_i, y_i=y_i) npt.assert_array_equal(WS_ilk.shape, (5, 360, 23)) # check probability local_wind()[-1] npt.assert_equal( site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[10])[-1], site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[10], wd_bin_size=2)[-1] * 180) npt.assert_almost_equal( site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[9, 10, 11])[-1].sum(), site.local_wind(x_i=x_i, y_i=y_i, wd=[0], ws=[10], ws_bin_size=3)[-1], 5) z = np.arange(1, 100) zero = [0] * len(z) ws = site.local_wind(x_i=zero, y_i=zero, h_i=z, wd=[0], ws=[10])[1][:, 0, 0] site.h_ref = 70 ws70 = site.local_wind(x_i=zero, y_i=zero, h_i=z, wd=[0], ws=[10])[1][:, 0, 0] if 0: import matplotlib.pyplot as plt plt.plot(ws, z) plt.plot(ws70, z) plt.show() npt.assert_array_equal(10 * (z / 50)**.3, ws) npt.assert_array_equal(10 * (z / 70)**.3, ws70)
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)