Skip to content

Commit

Permalink
Clean examples
Browse files Browse the repository at this point in the history
  • Loading branch information
uvchik committed Apr 12, 2023
1 parent 59835c3 commit f1649e4
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 19 deletions.
145 changes: 145 additions & 0 deletions example/simple_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
"""
The ``turbine_cluster_modelchain_example`` module shows how to calculate the
power output of wind farms and wind turbine clusters with the windpowerlib.
A cluster can be useful if you want to calculate the feed-in of a region for
which you want to use one single weather data point.
Functions that are used in the ``modelchain_example``, like the initialization
of wind turbines, are imported and used without further explanations.
SPDX-FileCopyrightText: 2019 oemof developer group <[email protected]>
SPDX-License-Identifier: MIT
"""
import pandas as pd
import requests
import os

from windpowerlib import WindFarm
from windpowerlib import WindTurbine
from windpowerlib import TurbineClusterModelChain
from windpowerlib import WindTurbineCluster

# You can use the logging package to get logging messages from the windpowerlib
# Change the logging level if you want more or less messages
import logging

logging.getLogger().setLevel(logging.INFO)


def get_weather_data(filename="weather.csv", **kwargs):
r"""
Imports weather data from a file.
The data include wind speed at two different heights in m/s, air
temperature in two different heights in K, surface roughness length in m
and air pressure in Pa. The height in m for which the data applies is
specified in the second row.
In case no weather data file exists, an example weather data file is
automatically downloaded and stored in the same directory as this example.
Parameters
----------
filename : str
Filename of the weather data file. Default: 'weather.csv'.
Other Parameters
----------------
datapath : str, optional
Path where the weather data file is stored.
Default is the same directory this example is stored in.
Returns
-------
:pandas:`pandas.DataFrame<frame>`
DataFrame with time series for wind speed `wind_speed` in m/s,
temperature `temperature` in K, roughness length `roughness_length`
in m, and pressure `pressure` in Pa.
The columns of the DataFrame are a MultiIndex where the first level
contains the variable name as string (e.g. 'wind_speed') and the
second level contains the height as integer at which it applies
(e.g. 10, if it was measured at a height of 10 m). The index is a
DateTimeIndex.
"""

if "datapath" not in kwargs:
kwargs["datapath"] = os.path.dirname(__file__)

file = os.path.join(kwargs["datapath"], filename)

# download example weather data file in case it does not yet exist
if not os.path.isfile(file):
logging.debug("Download weather data for example.")
req = requests.get("https://osf.io/59bqn/download")
with open(file, "wb") as fout:
fout.write(req.content)

# read csv file
weather_df = pd.read_csv(
file,
index_col=0,
header=[0, 1],
date_parser=lambda idx: pd.to_datetime(idx, utc=True),
)

# change time zone
weather_df.index = weather_df.index.tz_convert("Europe/Berlin")

return weather_df


def run_example():
r"""
Runs the example.
"""
weather = get_weather_data("weather.csv")
e126 = WindTurbine(
**{
"turbine_type": "E-126/4200", # turbine type as in register
"hub_height": 135, # in m
}
)
v90 = WindTurbine(
**{
"turbine_type": "V90/2000", # turbine name as in register
"hub_height": 120, # in m
}
)

# specification of wind farm data (2) containing a wind farm efficiency
# wind turbine fleet is provided using the to_group function
example_farm = WindFarm(
**{
"name": "example_farm_2",
"wind_turbine_fleet": [
v90.to_group(number_turbines=6),
e126.to_group(total_capacity=12.6e6),
],
"efficiency": 0.5
}
)

example_cluster = WindTurbineCluster(**{
"name": "Offshore_cluster",
"wind_farms": [example_farm, example_farm],
})

# ModelChain with wind farm
mc_farm = TurbineClusterModelChain(
example_farm, wake_losses_model=None,
).run_model(weather)
flh_farm = mc_farm.power_output.sum() / example_farm.nominal_power

# ModelChain with wind cluster
mc_cluster = TurbineClusterModelChain(
example_cluster, wake_losses_model=None,
).run_model(weather)
flh_cluster = mc_cluster.power_output.sum() / example_cluster.nominal_power

print("Full Load Hours of cluster:", flh_cluster)
print("Full Load Hours of farm:", flh_farm)


if __name__ == "__main__":
run_example()
24 changes: 12 additions & 12 deletions example/turbine_cluster_modelchain_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# Change the logging level if you want more or less messages
import logging

logging.getLogger().setLevel(logging.DEBUG)
# logging.getLogger().setLevel(logging.DEBUG)


def initialize_wind_farms(my_turbine, e126):
Expand Down Expand Up @@ -145,14 +145,15 @@ class that provides all necessary steps to calculate the power output of a
# power output calculation for example_farm
# initialize TurbineClusterModelChain with default parameters and use
# run_model method to calculate power output
mc_example_farm = TurbineClusterModelChain(example_farm).run_model(weather)
mc_example_farm = TurbineClusterModelChain(example_farm, wake_losses_model="wind_farm_efficiency").run_model(weather)
# write power output time series to WindFarm object
example_farm.power_output = mc_example_farm.power_output

# power output calculation for turbine_cluster
# own specifications for TurbineClusterModelChain setup
modelchain_data = {
"wake_losses_model": "wind_farm_efficiency", # 'dena_mean' (default), None,
"wake_losses_model": "wind_farm_efficiency",
# 'dena_mean' (default), None,
# 'wind_farm_efficiency' or name
# of another wind efficiency curve
# see :py:func:`~.wake_losses.get_wind_efficiency_curve`
Expand All @@ -164,15 +165,14 @@ class that provides all necessary steps to calculate the power output of a
"smoothing_order": "wind_farm_power_curves", #
# 'wind_farm_power_curves' (default) or
# 'turbine_power_curves'
"wind_speed_model": "logarithmic", # 'logarithmic' (default),
# 'hellman' or
# 'interpolation_extrapolation'
"density_model": "ideal_gas", # 'barometric' (default), 'ideal_gas' or
# 'interpolation_extrapolation'
"temperature_model": "linear_gradient", # 'linear_gradient' (def.) or
# 'interpolation_extrapolation'
"power_output_model": "power_curve", # 'power_curve' (default) or
# 'power_coefficient_curve'
"wind_speed_model": "logarithmic",
# 'logarithmic' (default), 'hellman' or 'interpolation_extrapolation'
"density_model": "ideal_gas", #
# 'barometric' (default), 'ideal_gas' or 'interpolation_extrapolation'
"temperature_model": "linear_gradient",
# 'linear_gradient' (def.) or 'interpolation_extrapolation'
"power_output_model": "power_curve",
# 'power_curve' (default) or 'power_coefficient_curve'
"density_correction": True, # False (default) or True
"obstacle_height": 0, # default: 0
"hellman_exp": None,
Expand Down
18 changes: 11 additions & 7 deletions windpowerlib/wind_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,11 @@ def assign_power_curve(
and kwargs["roughness_length"] is not None
):
# Calculate turbulence intensity and write to kwargs
turbulence_intensity = tools.estimate_turbulence_intensity(
row["wind_turbine"].hub_height,
kwargs["roughness_length"],
turbulence_intensity = (
tools.estimate_turbulence_intensity(
row["wind_turbine"].hub_height,
kwargs["roughness_length"],
)
)
kwargs["turbulence_intensity"] = turbulence_intensity
else:
Expand Down Expand Up @@ -463,10 +465,12 @@ def assign_power_curve(
)
if wake_losses_model == "wind_farm_efficiency":
if self.efficiency is not None:
wind_farm_power_curve = power_curves.wake_losses_to_power_curve(
wind_farm_power_curve["wind_speed"].values,
wind_farm_power_curve["value"].values,
wind_farm_efficiency=self.efficiency,
wind_farm_power_curve = (
power_curves.wake_losses_to_power_curve(
wind_farm_power_curve["wind_speed"].values,
wind_farm_power_curve["value"].values,
wind_farm_efficiency=self.efficiency,
)
)
else:
msg = (
Expand Down

0 comments on commit f1649e4

Please sign in to comment.