def site(): return UniformWeibullSite(f, A, k, ti, shear=PowerShear(50, alpha=np.zeros_like(f) + .3))
def main(): if __name__ == '__main__': f = [0.035972, 0.039487, 0.051674, 0.070002, 0.083645, 0.064348, 0.086432, 0.117705, 0.151576, 0.147379, 0.10012, 0.05166] A = [9.176929, 9.782334, 9.531809, 9.909545, 10.04269, 9.593921, 9.584007, 10.51499, 11.39895, 11.68746, 11.63732, 10.08803] k = [2.392578, 2.447266, 2.412109, 2.591797, 2.755859, 2.595703, 2.583984, 2.548828, 2.470703, 2.607422, 2.626953, 2.326172] ti = .1 h_ref = 100 alpha = .1 site = UniformWeibullSite(f, A, k, ti, shear=PowerShear(h_ref=h_ref, alpha=alpha)) x_i = y_i = np.arange(5) wdir_lst = np.arange(0, 360, 90) wsp_lst = np.arange(1, 20) WD_ilk, WS_ilk, TI_ilk, P_ilk = site.local_wind(x_i=x_i, y_i=y_i, wd=wdir_lst, ws=wsp_lst) import matplotlib.pyplot as plt site.plot_ws_distribution(0, 0, wdir_lst) plt.figure() z = np.arange(1, 100) u = [site.local_wind(x_i=[0], y_i=[0], h_i=[z_], wd=0, ws=10)[1][0][0] for z_ in z] plt.plot(u, z) plt.xlabel('Wind speed [m/s]') plt.ylabel('Height [m]') plt.show()
def test_plot_wd_distribution_with_ws_levels_xr(site): import xarray as xr ds = xr.Dataset(data_vars={ 'Sector_frequency': ('wd', f), 'Weibull_A': ('wd', A), 'Weibull_k': ('wd', k) }, coords={'wd': np.linspace(0, 360, len(f), endpoint=False)}) site2 = XRSite(ds, shear=PowerShear(h_ref=100, alpha=.2), interp_method='nearest') p = site2.plot_wd_distribution(n_wd=12, ws_bins=[0, 5, 10, 15, 20, 25]) if 0: plt.show() # print(np.round(p.values, 4).tolist()) npt.assert_array_almost_equal(p, [[0.0075, 0.0179, 0.0091, 0.0014, 0.0001], [0.0069, 0.0188, 0.0115, 0.0022, 0.0001], [0.0098, 0.025, 0.0142, 0.0025, 0.0001], [0.0109, 0.0339, 0.0214, 0.0036, 0.0001], [0.0114, 0.0411, 0.0271, 0.004, 0.0001], [0.0108, 0.0324, 0.0185, 0.0026, 0.0001], [0.0147, 0.0434, 0.0247, 0.0035, 0.0001], [0.0164, 0.0524, 0.0389, 0.0092, 0.0007], [0.0185, 0.0595, 0.0524, 0.0184, 0.0026], [0.0153, 0.0564, 0.054, 0.0191, 0.0024], [0.0103, 0.0386, 0.0369, 0.0127, 0.0015], [0.0092, 0.0231, 0.0152, 0.0038, 0.0004]], 4) plt.close('all')
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_uniform_local_wind(uniform_site): site = uniform_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, h_i=100, wd=wdir_lst, ws=wsp_lst) npt.assert_array_equal(lw.WS, 10) npt.assert_array_equal(lw.WD, wdir_lst) npt.assert_array_equal(lw.TI, 0.1) npt.assert_array_equal(lw.P, np.array([0.035972, 0.070002, 0.086432, 0.147379]) * 3) npt.assert_array_equal(site.elevation(x_i, y_i), 0) lw = site.local_wind(x_i=x_i, y_i=y_i, h_i=100) npt.assert_array_equal(lw.WD_ilk.shape, (1, 360, 1)) z = np.arange(1, 200) zero = [0] * len(z) ws100_2 = site.local_wind(x_i=zero, y_i=zero, h_i=z, wd=[0], ws=[10]).WS_ilk[:, 0, 0] site.shear = PowerShear(70, alpha=.3) ws70_3 = site.local_wind(x_i=zero, y_i=zero, h_i=z, wd=[0], ws=[10]).WS_ilk[:, 0, 0] if 0: plt.plot(ws100_2, z) plt.plot(ws70_3, z) plt.show() npt.assert_array_equal(10 * (z / 100)**.2, ws100_2) npt.assert_array_equal(10 * (z / 70)**.3, ws70_3)
def complex_ws_site(): ti = 0.1 P = weibull.cdf(np.array([3, 5, 7, 9, 11, 13]), 10, 2) - weibull.cdf(np.array([0, 3, 5, 7, 9, 11]), 10, 2) ds = xr.Dataset( data_vars={'Speedup': (['ws'], np.arange(.8, 1.4, .1)), 'P': (('wd', 'ws'), P[na, :] * np.array(f)[:, na]), 'TI': ti}, coords={'ws': [1.5, 4, 6, 8, 10, 12], 'wd': np.linspace(0, 360, len(f), endpoint=False)}) return XRSite(ds, shear=PowerShear(h_ref=100, alpha=.2), interp_method='linear')
def complex_grid_site(): ti = 0.1 ds = xr.Dataset( data_vars={'Speedup': (['x', 'y'], np.arange(.8, 1.4, .1).reshape((3, 2))), 'Turning': (['x', 'y'], np.arange(-2, 4, 1).reshape((3, 2))), 'Sector_frequency': ('wd', f), 'Weibull_A': ('wd', A), 'Weibull_k': ('wd', k), 'TI': ti}, coords={'x': [0, 5, 10], 'y': [0, 5], 'wd': np.linspace(0, 360, len(f), endpoint=False)}) return XRSite(ds, shear=PowerShear(h_ref=100, alpha=.2), interp_method='linear')
def complex_fixed_pos_site(): ti = 0.1 ds = xr.Dataset( data_vars={'Speedup': ('i', np.arange(.8, 1.3, .1)), 'Turning': ('i', np.arange(-2, 3)), 'Sector_frequency': ('wd', f), 'Weibull_A': ('wd', A), 'Weibull_k': ('wd', k), 'TI': ti}, coords={'i': np.arange(5), 'wd': np.linspace(0, 360, len(f), endpoint=False)}) x_i = np.arange(5) return XRSite(ds, initial_position=np.array([x_i, x_i + 1]).T, shear=PowerShear(h_ref=100, alpha=.2))
def test_wrong_height(): ti = 0.1 ds = xr.Dataset( data_vars={'Speedup': (['x', 'y', 'h'], np.arange(.8, 1.4, .1).reshape((3, 2, 1))), 'Sector_frequency': ('wd', f), 'Weibull_A': ('wd', A), 'Weibull_k': ('wd', k), 'TI': ti}, coords={'x': [0, 5, 10], 'y': [0, 5], 'h': [100], 'wd': np.linspace(0, 360, len(f), endpoint=False)}) site = XRSite(ds, shear=PowerShear(h_ref=100, alpha=.2), interp_method='linear') y = np.arange(5) x = y * 2 X, Y = np.meshgrid(x, y) x_i, y_i = X.flatten(), Y.flatten() 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, h_i=100, wd=wdir_lst, ws=wsp_lst)
def test_power_shear(): shear = PowerShear(70, alpha=[.1, .2]) h_lst = np.arange(10, 100, 10) u1, u2 = np.array([ shear(WS_ilk=[[[10]], [[10]]], WD_ilk=[[[0]], [[180]]], h_i=[h, h]) for h in h_lst ])[:, :, 0, 0].T if 0: import matplotlib.pyplot as plt plt.plot(u1, h_lst, label='alpha=0.1') plt.plot((h_lst / 70)**0.1 * 10, h_lst, ':') plt.plot(u2, h_lst, label='alpha=0.2') plt.plot((h_lst / 70)**0.2 * 10, h_lst, ':') plt.show() npt.assert_array_almost_equal( u1, [8.23, 8.82, 9.19, 9.46, 9.67, 9.85, 10., 10.13, 10.25], 2) npt.assert_array_almost_equal( u2, [6.78, 7.78, 8.44, 8.94, 9.35, 9.7, 10., 10.27, 10.52], 2)
def get_site(): f = [ 0.035972, 0.039487, 0.051674, 0.070002, 0.083645, 0.064348, 0.086432, 0.117705, 0.151576, 0.147379, 0.10012, 0.05166 ] A = [ 9.176929, 9.782334, 9.531809, 9.909545, 10.04269, 9.593921, 9.584007, 10.51499, 11.39895, 11.68746, 11.63732, 10.08803 ] k = [ 2.392578, 2.447266, 2.412109, 2.591797, 2.755859, 2.595703, 2.583984, 2.548828, 2.470703, 2.607422, 2.626953, 2.326172 ] ti = 0.001 h_ref = 100 alpha = .1 site = UniformWeibullSite(f, A, k, ti, shear=PowerShear(h_ref=h_ref, alpha=alpha)) spacing = 2000 N = 5 theta = 76 # deg dx = np.tan(np.radians(theta)) x = np.array([ np.linspace(0, (N - 1) * spacing, N) + i * spacing / dx for i in range(N) ]) y = np.array(np.array([N * [i * spacing] for i in range(N)])) initial_positions = np.column_stack((x.ravel(), y.ravel())) eps = 2000 delta = 5 site.boundary = np.array([ (0 - delta, 0 - delta), ((N - 1) * spacing + eps, 0 - delta), ((N - 1) * spacing * (1 + 1 / dx) + eps * (1 + np.cos(np.radians(theta))), (N - 1) * spacing + eps * np.sin(np.radians(theta)) - delta), ((N - 1) * spacing / dx + eps * np.cos(np.radians(theta)), (N - 1) * spacing + eps * np.sin(np.radians(theta))) ]) site.initial_position = initial_positions return site
def test_power_shear(): shear = PowerShear(70, alpha=[.1, .2]) h_lst = np.arange(10, 100, 10) site = UniformSite([1], .1) wref = site.wref([0, 180], [10]) h = xr.DataArray(np.arange(10, 100, 10), [('i', np.arange(9))]) u = shear(wref.WS, wref.WD, h) if 0: plt.plot(u.sel(wd=0), h, label='alpha=0.1') plt.plot((h_lst / 70)**0.1 * 10, h_lst, ':') plt.plot(u.sel(wd=180), h, label='alpha=0.2') plt.plot((h_lst / 70)**0.2 * 10, h_lst, ':') plt.legend() plt.show() npt.assert_array_almost_equal(u.sel( wd=0, ws=10), [8.23, 8.82, 9.19, 9.46, 9.67, 9.85, 10., 10.13, 10.25], 2) npt.assert_array_almost_equal(u.sel( wd=180, ws=10), [6.78, 7.78, 8.44, 8.94, 9.35, 9.7, 10., 10.27, 10.52], 2)
def uniform_weibull_site(): ti = 0.1 ds = xr.Dataset( data_vars={'Sector_frequency': ('wd', f), 'Weibull_A': ('wd', A), 'Weibull_k': ('wd', k), 'TI': ti}, coords={'wd': np.linspace(0, 360, len(f), endpoint=False)}) return XRSite(ds, shear=PowerShear(h_ref=100, alpha=.2))
def uniform_site(): ti = 0.1 ds = xr.Dataset( data_vars={'WS': 10, 'P': ('wd', f), 'TI': ti}, coords={'wd': np.linspace(0, 360, len(f), endpoint=False)}) return XRSite(ds, shear=PowerShear(h_ref=100, alpha=.2))