|
| 1 | +import windpowerlib.modelchain as mc |
| 2 | +import windpowerlib.wind_turbine as wt |
| 3 | +from pandas.util.testing import assert_series_equal |
| 4 | +from numpy.testing import assert_array_equal, assert_allclose |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | +import numpy as np |
| 8 | + |
| 9 | + |
| 10 | +class TestModelChain: |
| 11 | + |
| 12 | + @classmethod |
| 13 | + def setup_class(self): |
| 14 | + self.test_turbine = {'hub_height': 100, |
| 15 | + 'd_rotor': 80, |
| 16 | + 'turbine_name': 'ENERCON E 126 7500'} |
| 17 | + |
| 18 | + def test_v_wind_hub(self): |
| 19 | + # Test modelchain with wind_model='logarithmic' |
| 20 | + test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine), |
| 21 | + wind_model='logarithmic') |
| 22 | + # Test modelchain with wind_model='hellman' |
| 23 | + test_mc_2 = mc.ModelChain(wt.WindTurbine(**self.test_turbine), |
| 24 | + wind_model='hellman') |
| 25 | + weather = {'v_wind': pd.Series(data=[5.0, 6.5]), |
| 26 | + 'v_wind_2': pd.Series(data=[4.0, 5.0]), # TODO: test v_wind_2 is not in weather |
| 27 | + 'z0': 0.15} |
| 28 | + weather_df = pd.DataFrame(data={'v_wind': [5.0, 6.5], |
| 29 | + 'v_wind_2': [4.0, 5.0], |
| 30 | + 'z0': 0.15}, |
| 31 | + index=[0, 1]) |
| 32 | + weather_arr = {'v_wind': np.array(weather['v_wind']), |
| 33 | + 'v_wind_2': np.array(weather['v_wind_2']), |
| 34 | + 'z0': 0.15} |
| 35 | + data_height = {'v_wind': 10, |
| 36 | + 'v_wind_2': 8} |
| 37 | + |
| 38 | + # v_wind is closer to hub height than v_wind_2 # TODO: Add test for v_wind_2 is closer to hub height than v_wind |
| 39 | + v_wind_exp = pd.Series(data=[7.74137, 10.06377]) |
| 40 | + assert_series_equal(test_mc.v_wind_hub(weather, data_height), |
| 41 | + v_wind_exp) |
| 42 | + assert_series_equal(test_mc.v_wind_hub(weather_df, data_height), |
| 43 | + v_wind_exp) |
| 44 | + v_wind_exp = np.array([7.74136523, 10.0637748]) |
| 45 | + assert_allclose(test_mc.v_wind_hub(weather_arr, data_height), |
| 46 | + v_wind_exp) |
| 47 | + v_wind_exp = pd.Series(data=[7.12462, 9.26201]) |
| 48 | + assert_series_equal(test_mc_2.v_wind_hub(weather, data_height), |
| 49 | + v_wind_exp) |
| 50 | + assert_series_equal(test_mc_2.v_wind_hub(weather_df, data_height), |
| 51 | + v_wind_exp) |
| 52 | + v_wind_exp = np.array([7.12462437, 9.26201168]) |
| 53 | + assert_allclose(test_mc_2.v_wind_hub(weather_arr, data_height), |
| 54 | + v_wind_exp) |
| 55 | + |
| 56 | + # v_wind is given at hub height |
| 57 | + data_height['v_wind'] = 100 |
| 58 | + v_wind_exp = pd.Series(data=[5.0, 6.5]) |
| 59 | + assert_series_equal(test_mc.v_wind_hub(weather, data_height), |
| 60 | + v_wind_exp) |
| 61 | + assert_series_equal(test_mc.v_wind_hub(weather_df, data_height), |
| 62 | + v_wind_exp) |
| 63 | + v_wind_exp = np.array([5.0, 6.5]) |
| 64 | + assert_array_equal(test_mc_2.v_wind_hub(weather_arr, data_height), |
| 65 | + v_wind_exp) |
| 66 | + |
| 67 | + # v_wind_2 is given at hub height |
| 68 | + v_wind_exp = pd.Series(data=[4.0, 5.0]) |
| 69 | + data_height['v_wind'] = 10 |
| 70 | + data_height['v_wind_2'] = 100 |
| 71 | + assert_series_equal(test_mc_2.v_wind_hub(weather, data_height), |
| 72 | + v_wind_exp) |
| 73 | + assert_series_equal(test_mc_2.v_wind_hub(weather_df, data_height), |
| 74 | + v_wind_exp) |
| 75 | + v_wind_exp = np.array([4.0, 5.0]) |
| 76 | + assert_array_equal(test_mc_2.v_wind_hub(weather_arr, data_height), |
| 77 | + v_wind_exp) |
| 78 | + |
| 79 | + def test_rho_hub(self): |
| 80 | + # Test modelchain with rho_model='barometric' and |
| 81 | + # temperature_model='gradient' |
| 82 | + test_mc = mc.ModelChain(wt.WindTurbine(**self.test_turbine), |
| 83 | + rho_model='barometric', |
| 84 | + temperature_model='gradient') |
| 85 | + # Test modelchain with rho_model='ideal_gas' and |
| 86 | + # temperature_model='interpolation' |
| 87 | + test_mc_2 = mc.ModelChain(wt.WindTurbine(**self.test_turbine), |
| 88 | + rho_model='ideal_gas', |
| 89 | + temperature_model='interpolation') |
| 90 | + weather = {'temp_air': pd.Series(data=[267, 268]), |
| 91 | + 'temp_air_2': pd.Series(data=[267, 266]), # TODO: test temp_air_2 is not in weather |
| 92 | + 'pressure': pd.Series(data=[101125, 101000])} |
| 93 | + weather_df = pd.DataFrame(data={'temp_air': [267, 268], |
| 94 | + 'temp_air_2': [267, 266], |
| 95 | + 'pressure': [101125, 101000]}, |
| 96 | + index=[0, 1]) |
| 97 | + weather_arr = {'temp_air': np.array(weather['temp_air']), |
| 98 | + 'temp_air_2': np.array(weather['temp_air_2']), |
| 99 | + 'pressure': np.array(weather['pressure'])} |
| 100 | + data_height = {'temp_air': 2, |
| 101 | + 'temp_air_2': 10, |
| 102 | + 'pressure': 0} |
| 103 | + |
| 104 | + # temp_air_2 is closer to hub height than temp_air # TODO: Add test for temp_air is closer to hub height than temp_air_2 |
| 105 | + rho_exp = pd.Series(data=[1.30617, 1.29966]) |
| 106 | + assert_series_equal(test_mc.rho_hub(weather, data_height), rho_exp) |
| 107 | + assert_series_equal(test_mc.rho_hub(weather_df, data_height), rho_exp) |
| 108 | + rho_exp = np.array([1.30616958, 1.29965556]) |
| 109 | + assert_allclose(test_mc.rho_hub(weather_arr, data_height), rho_exp) |
| 110 | + rho_exp = pd.Series(data=[1.30309, 1.42707]) |
| 111 | + assert_series_equal(test_mc_2.rho_hub(weather, data_height), rho_exp) |
| 112 | + assert_series_equal(test_mc_2.rho_hub(weather_df, data_height), |
| 113 | + rho_exp) |
| 114 | + rho_exp = np.array([1.30309439, 1.42706674]) |
| 115 | + assert_allclose(test_mc_2.rho_hub(weather_arr, data_height), rho_exp) |
| 116 | + |
| 117 | + # temp_air at hub height |
| 118 | + rho_exp = pd.Series(data=[1.30305, 1.29657]) |
| 119 | + data_height['temp_air'] = 100 |
| 120 | + assert_series_equal(test_mc.rho_hub(weather, data_height), rho_exp) |
| 121 | + assert_series_equal(test_mc.rho_hub(weather_df, data_height), rho_exp) |
| 122 | + rho_exp = np.array([1.30305336, 1.29656645]) |
| 123 | + assert_allclose(test_mc.rho_hub(weather_arr, data_height), rho_exp) |
| 124 | + |
| 125 | + # temp_air_2 at hub height |
| 126 | + rho_exp = pd.Series(data=[1.30309, 1.30636]) |
| 127 | + data_height['temp_air'] = 2 |
| 128 | + data_height['temp_air_2'] = 100 |
| 129 | + assert_series_equal(test_mc_2.rho_hub(weather, data_height), rho_exp) |
| 130 | + assert_series_equal(test_mc_2.rho_hub(weather_df, data_height), |
| 131 | + rho_exp) |
| 132 | + rho_exp = np.array([1.30309439, 1.30635621]) |
| 133 | + assert_allclose(test_mc_2.rho_hub(weather_arr, data_height), rho_exp) |
| 134 | + |
| 135 | + def test_run_model(self): |
| 136 | + weather = {'temp_air': pd.Series(data=[267, 268]), |
| 137 | + 'temp_air_2': pd.Series(data=[267, 266]), |
| 138 | + 'v_wind': pd.Series(data=[5.0, 6.5]), |
| 139 | + 'v_wind_2': pd.Series(data=[4.0, 5.0]), |
| 140 | + 'pressure': pd.Series(data=[101125, 101000]), |
| 141 | + 'z0': 0.15} |
| 142 | + weather_df = pd.DataFrame(data={'v_wind': [5.0, 6.5], |
| 143 | + 'v_wind_2': [4.0, 5.0], |
| 144 | + 'z0': 0.15, |
| 145 | + 'temp_air': [267, 268], |
| 146 | + 'temp_air_2': [267, 266], |
| 147 | + 'pressure': [101125, 101000]}, |
| 148 | + index=[0, 1]) |
| 149 | + weather_arr = {'v_wind': np.array(weather['v_wind']), |
| 150 | + 'v_wind_2': np.array(weather['v_wind_2']), |
| 151 | + 'temp_air': np.array(weather['temp_air']), |
| 152 | + 'temp_air_2': np.array(weather['temp_air_2']), |
| 153 | + 'pressure': np.array(weather['pressure']), |
| 154 | + 'z0': np.array([0.15, 0.15])} |
| 155 | + data_height = {'temp_air': 2, |
| 156 | + 'temp_air_2': 10, |
| 157 | + 'v_wind': 10, |
| 158 | + 'v_wind_2': 8, |
| 159 | + 'pressure': 0} |
| 160 | + test_turbine = {'hub_height': 100, |
| 161 | + 'd_rotor': 80, |
| 162 | + 'turbine_name': 'ENERCON E 126 7500', |
| 163 | + 'fetch_curve': 'p'} |
| 164 | + test_modelchain = {'wind_model': 'hellman', |
| 165 | + 'rho_model': 'barometric', |
| 166 | + 'temperature_model': 'interpolation', |
| 167 | + 'power_output_model': 'p_values', |
| 168 | + 'density_corr': True} |
| 169 | + |
| 170 | + # Test with default parameters of modelchain (p curve) |
| 171 | + power_output_exp = pd.Series(data=[1731887.39768, 3820152.27489], |
| 172 | + name='feedin_wind_turbine') |
| 173 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine)) |
| 174 | + test_mc.run_model(weather, data_height) |
| 175 | + assert_series_equal(test_mc.power_output, power_output_exp) |
| 176 | + |
| 177 | + # Test with density corrected power curve |
| 178 | + power_output_exp = pd.Series(data=[1430312.76771, 3746075.21279], |
| 179 | + name='feedin_wind_turbine') |
| 180 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 181 | + **test_modelchain) |
| 182 | + test_mc.run_model(weather, data_height) |
| 183 | + assert_series_equal(test_mc.power_output, power_output_exp) |
| 184 | + |
| 185 | + # Test with power coefficient curve |
| 186 | + power_output_exp = pd.Series(data=[557835.45403, 1363746.94496], |
| 187 | + name='feedin_wind_turbine') |
| 188 | + test_turbine['fetch_curve'] = 'cp' |
| 189 | + test_modelchain['power_output_model'] = 'cp_values' |
| 190 | + test_modelchain['density_corr'] = False |
| 191 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 192 | + **test_modelchain) |
| 193 | + test_mc.run_model(weather, data_height) |
| 194 | + assert_series_equal(test_mc.power_output, power_output_exp) |
| 195 | + |
| 196 | + # Ideal gas equation and density corrected power coefficient curve |
| 197 | + power_output_exp = pd.Series(data=[567683.92454, 1485556.96435], |
| 198 | + name='feedin_wind_turbine') |
| 199 | + test_modelchain['rho_model'] = 'ideal_gas' |
| 200 | + test_modelchain['density_corr'] = True |
| 201 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 202 | + **test_modelchain) |
| 203 | + test_mc.run_model(weather, data_height) |
| 204 | + assert_series_equal(test_mc.power_output, power_output_exp) |
| 205 | + |
| 206 | + # Test weather as DataFrame |
| 207 | + test_mc.run_model(weather_df, data_height) |
| 208 | + assert_series_equal(test_mc.power_output, power_output_exp) |
| 209 | + |
| 210 | + # Test weather dictionary with numpy.arrays |
| 211 | + power_output_exp = pd.Series(data=[567683.92454, 1485556.96435], |
| 212 | + index=[1, 2], name='feedin_wind_turbine') |
| 213 | + test_mc.run_model(weather_arr, data_height) |
| 214 | + assert_series_equal(test_mc.power_output, power_output_exp) |
| 215 | + |
| 216 | + # Raise ValueErrors due to wrong spelling of parameters |
| 217 | + with pytest.raises(ValueError): |
| 218 | + test_modelchain['power_output_model'] = 'wrong_spelling' |
| 219 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 220 | + **test_modelchain) |
| 221 | + test_mc.run_model(weather, data_height) |
| 222 | + with pytest.raises(ValueError): |
| 223 | + test_modelchain['power_output_model'] = 'cp_values' |
| 224 | + test_modelchain['wind_model'] = 'wrong_spelling' |
| 225 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 226 | + **test_modelchain) |
| 227 | + test_mc.run_model(weather, data_height) |
| 228 | + with pytest.raises(ValueError): |
| 229 | + test_modelchain['wind_model'] = 'hellman' |
| 230 | + test_modelchain['rho_model'] = 'wrong_spelling' |
| 231 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 232 | + **test_modelchain) |
| 233 | + test_mc.run_model(weather, data_height) |
| 234 | + with pytest.raises(ValueError): |
| 235 | + test_modelchain['rho_model'] = 'barometric' |
| 236 | + test_modelchain['temperature_model'] = 'wrong_spelling' |
| 237 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 238 | + **test_modelchain) |
| 239 | + test_mc.run_model(weather, data_height) |
| 240 | + |
| 241 | + # Raise TypeErrors due to wrong type of `density_corr` |
| 242 | + with pytest.raises(TypeError): |
| 243 | + test_modelchain['temperature_model'] = 'gradient' |
| 244 | + test_modelchain['density_corr'] = 'wrong_type' |
| 245 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 246 | + **test_modelchain) |
| 247 | + test_mc.run_model(weather, data_height) |
| 248 | + with pytest.raises(TypeError): |
| 249 | + test_modelchain['power_output_model'] = 'cp_values' |
| 250 | + test_modelchain['density_corr'] = 'wrong_type' |
| 251 | + test_mc = mc.ModelChain(wt.WindTurbine(**test_turbine), |
| 252 | + **test_modelchain) |
| 253 | + test_mc.run_model(weather, data_height) |
| 254 | + |
| 255 | + # Raise TypeErrors due to missing cp- or p-values |
| 256 | + with pytest.raises(TypeError): |
| 257 | + turbine1 = {'hub_height': 100, |
| 258 | + 'd_rotor': 80, |
| 259 | + 'turbine_name': 'ENERCON E 126 7500', |
| 260 | + 'fetch_curve': 'p'} |
| 261 | + modelchain1 = {'wind_model': 'hellman', |
| 262 | + 'rho_model': 'barometric', |
| 263 | + 'temperature_model': 'interpolation', |
| 264 | + 'power_output_model': 'cp_values', |
| 265 | + 'density_corr': True} |
| 266 | + test_mc = mc.ModelChain(wt.WindTurbine(**turbine1), |
| 267 | + **modelchain1) |
| 268 | + test_mc.run_model(weather, data_height) |
| 269 | + with pytest.raises(TypeError): |
| 270 | + turbine2 = {'hub_height': 100, |
| 271 | + 'd_rotor': 80, |
| 272 | + 'turbine_name': 'ENERCON E 126 7500', |
| 273 | + 'fetch_curve': 'cp'} |
| 274 | + modelchain2 = {'wind_model': 'hellman', |
| 275 | + 'rho_model': 'barometric', |
| 276 | + 'temperature_model': 'interpolation', |
| 277 | + 'power_output_model': 'p_values', |
| 278 | + 'density_corr': True} |
| 279 | + test_mc = mc.ModelChain(wt.WindTurbine(**turbine2), |
| 280 | + **modelchain2) |
| 281 | + test_mc.run_model(weather, data_height) |
0 commit comments