# Example: Optimize yaw for a single wind speed and multiple wind directions#

"""Example: Optimize yaw for a single wind speed and multiple wind directions

Use the serial-refine method to optimize the yaw angles for a 3-turbine wind farm

"""

import matplotlib.pyplot as plt
import numpy as np

from floris import FlorisModel, TimeSeries
from floris.optimization.yaw_optimization.yaw_optimizer_sr import YawOptimizationSR

# Load the default example floris object
fmodel = FlorisModel("../inputs/gch.yaml")

# Define an inflow that
# keeps wind speed and TI constant while sweeping the wind directions
wind_directions = np.arange(0.0, 360.0, 3.0)
time_series = TimeSeries(
wind_directions=wind_directions,
wind_speeds=8.0,
turbulence_intensities=0.06,
)

# Reinitialize as a 3-turbine using the above inflow
D = 126.0 # Rotor diameter for the NREL 5 MW
fmodel.set(
layout_x=[0.0, 5 * D, 10 * D],
layout_y=[0.0, 0.0, 0.0],
wind_data=time_series,
)

# Initialize optimizer object and run optimization using the Serial-Refine method
yaw_opt = YawOptimizationSR(fmodel)
df_opt = yaw_opt.optimize()

print("Optimization results:")
print(df_opt)

# Split out the turbine results
for t in range(3):
df_opt['t%d' % t] = df_opt.yaw_angles_opt.apply(lambda x: x[t])

# Show the results
fig, axarr = plt.subplots(2,1,sharex=True,sharey=False,figsize=(8,8))

# Yaw results
ax = axarr[0]
for t in range(3):
ax.plot(df_opt.wind_direction,df_opt['t%d' % t],label='t%d' % t)
ax.set_ylabel('Yaw Offset (deg')
ax.legend()
ax.grid(True)

# Power results
ax = axarr[1]
ax.plot(df_opt.wind_direction,df_opt.farm_power_baseline,color='k',label='Baseline Farm Power')
ax.plot(df_opt.wind_direction,df_opt.farm_power_opt,color='r',label='Optimized Farm Power')
ax.set_ylabel('Power (W)')
ax.set_xlabel('Wind Direction (deg)')
ax.legend()
ax.grid(True)

plt.show()
import warnings
warnings.filterwarnings('ignore')

[Serial Refine] Processing pass=0, turbine_depth=0 (0.0%)
[Serial Refine] Processing pass=0, turbine_depth=1 (16.7%)

[Serial Refine] Processing pass=0, turbine_depth=2 (33.3%)
[Serial Refine] Processing pass=1, turbine_depth=0 (50.0%)
[Serial Refine] Processing pass=1, turbine_depth=1 (66.7%)

[Serial Refine] Processing pass=1, turbine_depth=2 (83.3%)
Optimization results:
wind_direction  wind_speed  turbulence_intensity   yaw_angles_opt  \
0               0.0         8.0                  0.06  [0.0, 0.0, 0.0]
1               3.0         8.0                  0.06  [0.0, 0.0, 0.0]
2               6.0         8.0                  0.06  [0.0, 0.0, 0.0]
3               9.0         8.0                  0.06  [0.0, 0.0, 0.0]
4              12.0         8.0                  0.06  [0.0, 0.0, 0.0]
..              ...         ...                   ...              ...
115           345.0         8.0                  0.06  [0.0, 0.0, 0.0]
116           348.0         8.0                  0.06  [0.0, 0.0, 0.0]
117           351.0         8.0                  0.06  [0.0, 0.0, 0.0]
118           354.0         8.0                  0.06  [0.0, 0.0, 0.0]
119           357.0         8.0                  0.06  [0.0, 0.0, 0.0]

farm_power_opt  farm_power_baseline
0      5.261863e+06         5.261863e+06
1      5.261863e+06         5.261863e+06
2      5.261863e+06         5.261863e+06
3      5.261863e+06         5.261863e+06
4      5.261863e+06         5.261863e+06
..              ...                  ...
115    5.261863e+06         5.261863e+06
116    5.261863e+06         5.261863e+06
117    5.261863e+06         5.261863e+06
118    5.261863e+06         5.261863e+06
119    5.261863e+06         5.261863e+06

[120 rows x 6 columns]