Skip to content

Commit

Permalink
Refactor examples (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulf81 authored Apr 5, 2024
1 parent 99161f2 commit 53c1de2
Show file tree
Hide file tree
Showing 99 changed files with 3,774 additions and 2,979 deletions.
25 changes: 14 additions & 11 deletions .github/workflows/check-working-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@ jobs:
error_found=0 # 0 is false
error_results="Error in example:"
# Run each Python script example
for i in *.py; do
# Skip these examples until the wind rose, optimization package, and
# uncertainty interface are update to v4
if [[ $i == *20* ]]; then
continue
# Now run the examples in root and subdirectories
echo "Running examples"
for d in . $(find . -type d -name "*examples*"); do
cd $d
echo "========================= Example directory- $d"
for i in *.py; do
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Running example- $i"
if ! python $i; then
error_results="${error_results}"$'\n'" - ${i}"
error_found=1
fi
done
if [ "$d" != "." ]; then
cd ..
fi
if ! python $i; then
error_results="${error_results}"$'\n'" - ${i}"
error_found=1
fi
done
if [[ $error_found ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Example 1: Opening FLORIS and Computing Power
This first example illustrates several of the key concepts in FLORIS. It:
This example illustrates several of the key concepts in FLORIS. It demonstrates:
1) Initializing FLORIS
1) Initializing a FLORIS model
2) Changing the wind farm layout
3) Changing the incoming wind speed, wind direction and turbulence intensity
4) Running the FLORIS simulation
Expand All @@ -17,22 +17,22 @@
from floris import FlorisModel


# Initialize FLORIS with the given input file.
# The Floris class is the entry point for most usage.
# The FlorisModel class is the entry point for most usage.
# Initialize using an input yaml file
fmodel = FlorisModel("inputs/gch.yaml")

# Changing the wind farm layout uses FLORIS' set method to a two-turbine layout
fmodel.set(layout_x=[0, 500.0], layout_y=[0.0, 0.0])

# Changing wind speed, wind direction, and turbulence intensity using the set method
# Changing wind speed, wind direction, and turbulence intensity uses the set method
# as well. Note that the wind_speeds, wind_directions, and turbulence_intensities
# are all specified as arrays of the same length.
fmodel.set(wind_directions=np.array([270.0]),
wind_speeds=[8.0],
turbulence_intensities=np.array([0.06]))
fmodel.set(
wind_directions=np.array([270.0]), wind_speeds=[8.0], turbulence_intensities=np.array([0.06])
)

# Note that typically all 3, wind_directions, wind_speeds and turbulence_intensities
# must be supplied to set. However, the exception is if not changing the lenght
# must be supplied to set. However, the exception is if not changing the length
# of the arrays, then only one or two may be supplied.
fmodel.set(turbulence_intensities=np.array([0.07]))

Expand All @@ -42,9 +42,11 @@
# be unique. Internally in FLORIS, most data structures will have the findex as their
# 0th dimension. The value n_findex is the total number of conditions to be simulated.
# This command would simulate 4 conditions (n_findex = 4).
fmodel.set(wind_directions=np.array([270.0, 270.0, 270.0, 270.0]),
wind_speeds=[8.0, 8.0, 10.0, 10.0],
turbulence_intensities=np.array([0.06, 0.06, 0.06, 0.06]))
fmodel.set(
wind_directions=np.array([270.0, 270.0, 270.0, 270.0]),
wind_speeds=[8.0, 8.0, 10.0, 10.0],
turbulence_intensities=np.array([0.06, 0.06, 0.06, 0.06]),
)

# After the set method, the run method is called to perform the simulation
fmodel.run()
Expand Down
94 changes: 94 additions & 0 deletions examples/002_visualizations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""Example 2: Visualizations
This example demonstrates the use of the flow and layout visualizations in FLORIS.
First, an example wind farm layout is plotted, with the turbine names and the directions
and distances between turbines shown in different configurations by subplot.
Next, the horizontal flow field at hub height is plotted for a single wind condition.
FLORIS includes two modules for visualization:
1) flow_visualization: for visualizing the flow field
2) layout_visualization: for visualizing the layout of the wind farm
The two modules can be used together to visualize the flow field and the layout
of the wind farm.
"""


import matplotlib.pyplot as plt

import floris.layout_visualization as layoutviz
from floris import FlorisModel
from floris.flow_visualization import visualize_cut_plane


fmodel = FlorisModel("inputs/gch.yaml")

# Set the farm layout to have 8 turbines irregularly placed
layout_x = [0, 500, 0, 128, 1000, 900, 1500, 1250]
layout_y = [0, 300, 750, 1400, 0, 567, 888, 1450]
fmodel.set(layout_x=layout_x, layout_y=layout_y)


# Layout visualization contains the functions for visualizing the layout:
# plot_turbine_points
# plot_turbine_labels
# plot_turbine_rotors
# plot_waking_directions
# Each of which can be overlaid to provide further information about the layout
# This series of 4 subplots shows the different ways to visualize the layout

# Create the plotting objects using matplotlib
fig, axarr = plt.subplots(2, 2, figsize=(15, 10), sharex=False)
axarr = axarr.flatten()

ax = axarr[0]
layoutviz.plot_turbine_points(fmodel, ax=ax)
ax.set_title("Turbine Points")

ax = axarr[1]
layoutviz.plot_turbine_points(fmodel, ax=ax)
layoutviz.plot_turbine_labels(fmodel, ax=ax)
ax.set_title("Turbine Points and Labels")

ax = axarr[2]
layoutviz.plot_turbine_points(fmodel, ax=ax)
layoutviz.plot_turbine_labels(fmodel, ax=ax)
layoutviz.plot_waking_directions(fmodel, ax=ax, limit_num=2)
ax.set_title("Turbine Points, Labels, and Waking Directions")

# In the final subplot, use provided turbine names in place of the t_index
ax = axarr[3]
turbine_names = ["T1", "T2", "T3", "T4", "T9", "T10", "T75", "T78"]
layoutviz.plot_turbine_points(fmodel, ax=ax)
layoutviz.plot_turbine_labels(fmodel, ax=ax, turbine_names=turbine_names)
layoutviz.plot_waking_directions(fmodel, ax=ax, limit_num=2)
ax.set_title("Use Provided Turbine Names")


# Visualizations of the flow field are made by using calculate plane methods. In this example
# we show the horizontal plane at hub height, further examples are provided within
# the examples_visualizations folder

# For flow visualizations, the FlorisModel must be set to run a single condition
# (n_findex = 1)
fmodel.set(wind_speeds=[8.0], wind_directions=[290.0], turbulence_intensities=[0.06])
horizontal_plane = fmodel.calculate_horizontal_plane(
x_resolution=200,
y_resolution=100,
height=90.0,
)

# Plot the flow field with rotors
fig, ax = plt.subplots()
visualize_cut_plane(
horizontal_plane,
ax=ax,
label_contours=False,
title="Horizontal Flow with Turbine Rotors and labels",
)

# Plot the turbine rotors
layoutviz.plot_turbine_rotors(fmodel, ax=ax)
layoutviz.plot_turbine_labels(fmodel, ax=ax, turbine_names=turbine_names)

plt.show()
Loading

0 comments on commit 53c1de2

Please sign in to comment.