Skip to content

Commit 6553e1c

Browse files
committed
Merge branch 'features/tests' into features/tools
dieser
1 parent 784a8ff commit 6553e1c

File tree

10 files changed

+391
-227
lines changed

10 files changed

+391
-227
lines changed

example/basic_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def read_weather_data(filename, datetime_column='time_index',
6767
'pressure': 0,
6868
'temp_air': 2,
6969
'v_wind': 10,
70+
'Z0': 0,
7071
'temp_air_2': 10,
7172
'v_wind_2': 80}
7273

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class TestDensityTemperature:
1010

1111
@classmethod
12-
def setUpClass(self):
12+
def setup_class(self):
1313
self.h_hub = 100
1414
self.weather = {'temp_air': pd.Series(data=[267, 268]),
1515
'temp_air_2': pd.Series(data=[267, 266]),
@@ -25,23 +25,31 @@ def test_temperature_gradient(self):
2525
self.data_height['temp_air'],
2626
self.h_hub),
2727
temp_hub_exp)
28-
# Test array
29-
assert_array_equal(temperature_gradient(
30-
np.array(self.weather['temp_air']), self.data_height['temp_air'],
31-
self.h_hub), temp_hub_exp)
28+
# Test numpy array
29+
assert_array_equal(
30+
temperature_gradient(np.array(self.weather['temp_air']),
31+
self.data_height['temp_air'],
32+
self.h_hub),
33+
temp_hub_exp)
3234

3335
def test_temperature_interpol(self):
3436
# Test pandas.Series
3537
temp_hub_exp = pd.Series(data=[267.0, 243.5])
36-
assert_series_equal(temperature_interpol(
37-
self.weather['temp_air'], self.weather['temp_air_2'],
38-
self.data_height['temp_air'], self.data_height['temp_air_2'],
39-
self.h_hub), temp_hub_exp)
40-
# Test array
41-
assert_array_equal(temperature_interpol(
42-
np.array(self.weather['temp_air']),
43-
np.array(self.weather['temp_air_2']), self.data_height['temp_air'],
44-
self.data_height['temp_air_2'], self.h_hub), temp_hub_exp)
38+
assert_series_equal(
39+
temperature_interpol(self.weather['temp_air'],
40+
self.weather['temp_air_2'],
41+
self.data_height['temp_air'],
42+
self.data_height['temp_air_2'],
43+
self.h_hub),
44+
temp_hub_exp)
45+
# Test numpy array
46+
assert_array_equal(
47+
temperature_interpol(np.array(self.weather['temp_air']),
48+
np.array(self.weather['temp_air_2']),
49+
self.data_height['temp_air'],
50+
self.data_height['temp_air_2'],
51+
self.h_hub),
52+
temp_hub_exp)
4553

4654
def test_rho_barometric(self):
4755
# Test pandas.Series
@@ -51,7 +59,7 @@ def test_rho_barometric(self):
5159
self.h_hub,
5260
self.weather['temp_air']),
5361
rho_exp)
54-
# Test array
62+
# Test numpy array
5563
assert_allclose(rho_barometric(np.array(self.weather['pressure']),
5664
self.data_height['pressure'],
5765
self.h_hub,
@@ -66,7 +74,9 @@ def test_rho_ideal_gas(self):
6674
self.h_hub,
6775
self.weather['temp_air']),
6876
rho_exp)
69-
# Test array
70-
assert_allclose(rho_ideal_gas(
71-
np.array(self.weather['pressure']), self.data_height['pressure'],
72-
self.h_hub, np.array(self.weather['temp_air'])), rho_exp)
77+
# Test numpy array
78+
assert_allclose(rho_ideal_gas(np.array(self.weather['pressure']),
79+
self.data_height['pressure'],
80+
self.h_hub,
81+
np.array(self.weather['temp_air'])),
82+
rho_exp)

tests/test_modelchain.py

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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

Comments
 (0)