Skip to content

Commit

Permalink
Merge pull request #146 from OnSSET/vectorize-wind-cf
Browse files Browse the repository at this point in the history
Vectorized calc_wind_cfs
  • Loading branch information
AndreasSahlberg committed Jan 11, 2022
2 parents d2bf066 + a7271d1 commit e1f9c37
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
34 changes: 11 additions & 23 deletions onsset/onsset.py
Expand Up @@ -871,13 +871,8 @@ def grid_penalties(self, data_frame):
return c

@staticmethod
def get_wind_cf(wind_velocity):
"""Calculate the wind capacity factor based on the average wind velocity.
Parameters
----------
wind_velocity : float
"""
def calc_wind_cfs(wind_vel):
logging.info('Calculate Wind CF')

mu = 0.97 # availability factor
t = 8760
Expand All @@ -889,25 +884,18 @@ def get_wind_cf(wind_velocity):
p_curve = [0, 0, 0, 0, 30, 77, 135, 208, 287, 371, 450, 514, 558,
582, 594, 598, 600, 600, 600, 600, 600, 600, 600, 600, 600]

if wind_velocity == 0:
return 0
elif wind_velocity < 0:
raise ValueError('Wind velocity must be greater than 0')

else:
# Adjust for the correct hub height
alpha = (0.37 - 0.088 * log(wind_velocity)) / (1 - 0.088 * log(zr / 10))
u_z = wind_velocity * (z / zr) ** alpha
wind_speed = np.where(wind_vel > 0, wind_vel, 99)

# Rayleigh distribution and sum of series
rayleigh = [(pi / 2) * (u / u_z ** 2) * exp((-pi / 4) * (u / u_z) ** 2) for u in u_arr]
energy_produced = sum([mu * es * t * p * r for p, r in zip(p_curve, rayleigh)])
# Adjust for the correct hub height
alpha = (0.37 - 0.088 * np.log(wind_speed)) / (1 - 0.088 * log(zr / 10))
u_z = wind_speed * (z / zr) ** alpha

return energy_produced / (p_rated * t)
# Rayleigh distribution and sum of series
rayleigh = [(pi / 2) * (u / u_z ** 2) * np.exp((-pi / 4) * (u / u_z) ** 2) for u in u_arr]
energy_produced = sum([mu * es * t * p * r for p, r in zip(p_curve, rayleigh)])

def calc_wind_cfs(self):
logging.info('Calculate Wind CF')
return self.df[SET_WINDVEL].apply(self.get_wind_cf)
cf = np.where(wind_vel > 0, energy_produced / (p_rated * t), 0)
return cf

def prepare_wtf_tier_columns(self, num_people_per_hh_rural, num_people_per_hh_urban,
tier_1, tier_2, tier_3, tier_4, tier_5):
Expand Down
4 changes: 2 additions & 2 deletions onsset/runner.py
Expand Up @@ -5,7 +5,7 @@

import pandas as pd
from onsset import (SET_ELEC_ORDER, SET_LCOE_GRID, SET_MIN_GRID_DIST, SET_GRID_PENALTY,
SET_MV_CONNECT_DIST, SET_WINDCF, SettlementProcessor, Technology)
SET_MV_CONNECT_DIST, SET_WINDVEL, SET_WINDCF, SettlementProcessor, Technology)

try:
from onsset.specs import (SPE_COUNTRY, SPE_ELEC, SPE_ELEC_MODELLED,
Expand Down Expand Up @@ -61,7 +61,7 @@ def calibration(specs_path, csv_path, specs_path_calib, calibrated_csv_path):
onsseter.condition_df()
onsseter.df[SET_GRID_PENALTY] = onsseter.grid_penalties(onsseter.df)

onsseter.df[SET_WINDCF] = onsseter.calc_wind_cfs()
onsseter.df[SET_WINDCF] = onsseter.calc_wind_cfs(onsseter.df[SET_WINDVEL])

pop_actual = specs_data.loc[0, SPE_POP]
urban_current = specs_data.loc[0, SPE_URBAN]
Expand Down
11 changes: 7 additions & 4 deletions test/test_wind_cfs.py
Expand Up @@ -30,7 +30,7 @@ def positive_wind_speed(self, setup_settlementprocessor):
sp = setup_settlementprocessor
wind_vel = 5.08063

actual = sp.get_wind_cf(wind_vel)
actual = sp.calc_wind_cfs(wind_vel)

print(actual)

Expand All @@ -53,7 +53,7 @@ def test_calc_wind_cfs_zero(self, setup_settlementprocessor):
sp = setup_settlementprocessor
wind_vel = 0

actual = sp.get_wind_cf(wind_vel)
actual = sp.calc_wind_cfs(wind_vel)

print(actual)

Expand All @@ -76,5 +76,8 @@ def test_calc_wind_cfs_negative(self, setup_settlementprocessor):
sp = setup_settlementprocessor
wind_vel = -1

with raises(ValueError):
sp.get_wind_cf(wind_vel)
actual = sp.calc_wind_cfs(wind_vel)

expected = 0

assert actual == expected

0 comments on commit e1f9c37

Please sign in to comment.