Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import pulp
|
|
| 6 |
import plotly.graph_objs as go
|
| 7 |
from plotly.subplots import make_subplots
|
| 8 |
import plotly.express as px
|
|
|
|
| 9 |
|
| 10 |
# Function to fetch renewable energy data
|
| 11 |
def get_renewable_energy_data(city_code):
|
|
@@ -54,11 +55,13 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 54 |
- yearly_demand (float): The total yearly demand in TWh/year.
|
| 55 |
Returns:
|
| 56 |
- fig (Figure): A plotly figure showing the optimized energy dispatch and state of charge (SOC) over time.
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
data, error = get_renewable_energy_data(city_code)
|
| 59 |
if error:
|
| 60 |
st.error(error)
|
| 61 |
-
return None
|
| 62 |
|
| 63 |
# Convert capacity factor columns to numeric values, handling any errors and filling missing data.
|
| 64 |
for col in data.columns[1:]:
|
|
@@ -136,14 +139,18 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 136 |
battery_discharge_values = [battery_discharge[t].varValue for t in time_steps]
|
| 137 |
battery_charge_values = [-battery_charge[t].varValue for t in time_steps]
|
| 138 |
SOC_values = [SOC[t].varValue for t in time_steps]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Normalize state of charge (SOC) to a percentage.
|
| 141 |
max_SOC = max(SOC_values)
|
| 142 |
SOC_normalized = [(soc / max_SOC) * 100 for soc in SOC_values] if max_SOC > 0 else [0] * len(SOC_values)
|
| 143 |
|
| 144 |
-
# Create a subplot with
|
| 145 |
-
fig = make_subplots(rows=
|
| 146 |
-
subplot_titles=("Power supply and demand", "State of charge (Battery)"))
|
| 147 |
|
| 148 |
# Plot energy dispatch in the first row.
|
| 149 |
fig.add_trace(go.Scatter(x=data['Time'], y=supply_solar, mode='lines', stackgroup='one', name='Solar', line=dict(color='#FFD700', width=0)), row=1, col=1) # Solar: Gold
|
|
@@ -153,10 +160,14 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 153 |
fig.add_trace(go.Scatter(x=data['Time'], y=battery_discharge_values, mode='lines', stackgroup='one', name='Battery Discharge', fill='tonexty', line=dict(color='#6A3D9A', width=0)), row=1, col=1) # Battery Discharge: Brown
|
| 154 |
fig.add_trace(go.Scatter(x=data['Time'], y=battery_charge_values, mode='lines', stackgroup='two', name='Battery Charge', fill='tonexty', line=dict(color='#6A3D9A', width=0)), row=1, col=1) # Battery Charge: Purple
|
| 155 |
fig.add_trace(go.Scatter(x=data['Time'], y=-demand, mode='lines', stackgroup='two', name='Demand', line=dict(color='black', width=0)), row=1, col=1) # Demand: Black
|
|
|
|
| 156 |
|
| 157 |
# Plot state of charge (SOC) in the second row, normalized to 100%.
|
| 158 |
fig.add_trace(go.Scatter(x=data['Time'], y=SOC_normalized, mode='lines', name='State of Charge (SOC) - Normalized', line=dict(color='black')), row=2, col=1)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
| 160 |
# Layout settings for the figure.
|
| 161 |
fig.update_layout(
|
| 162 |
title_text='Optimized result',
|
|
@@ -170,12 +181,14 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 170 |
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 171 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 172 |
)
|
| 173 |
-
# Update the y-axis
|
| 174 |
fig.update_yaxes(title_text="State of Charge (%)", row=2, col=1)
|
|
|
|
| 175 |
|
| 176 |
-
return fig
|
| 177 |
|
| 178 |
# Streamlit UI for the application
|
|
|
|
| 179 |
st.title('Renewable Energy System Optimization')
|
| 180 |
|
| 181 |
# Explanation of the project
|
|
@@ -185,27 +198,29 @@ This application is designed to help researchers and policymakers explore and op
|
|
| 185 |
|
| 186 |
The optimization problem is solved using linear programming, ensuring a balance between supply and demand, and incorporating battery energy storage to manage intermittency issues inherent in renewable energy.
|
| 187 |
|
| 188 |
-
The visualizations provided help to better understand how different energy sources contribute to the overall power supply
|
| 189 |
""")
|
| 190 |
|
| 191 |
# Input fields for the user
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
| 199 |
|
| 200 |
# Button to trigger optimization
|
| 201 |
-
if st.button('Calculate'):
|
| 202 |
-
fig = optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wind_cost, river_cost, battery_cost, yearly_demand)
|
| 203 |
if fig:
|
| 204 |
-
st.plotly_chart(fig)
|
| 205 |
|
| 206 |
# Additional analysis and visualizations
|
| 207 |
st.markdown("### Additional Analysis")
|
| 208 |
-
st.markdown("The following plots provide additional insights into the renewable energy mix and
|
| 209 |
|
| 210 |
# Data for additional visualizations
|
| 211 |
renewable_data = {
|
|
@@ -217,14 +232,24 @@ if st.button('Calculate'):
|
|
| 217 |
cost_df = pd.DataFrame(list(renewable_data.items()), columns=['Technology', 'Cost (¥/MW)'])
|
| 218 |
|
| 219 |
# Plot cost comparison
|
| 220 |
-
fig_cost = px.bar(cost_df, x='Technology', y='Cost (¥/MW)', color='Technology', title='Cost Comparison of Different Renewable Technologies')
|
| 221 |
-
st.plotly_chart(fig_cost)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# Correlation analysis of renewable capacity factors
|
| 224 |
-
if city_code:
|
| 225 |
st.markdown("### Correlation Between Renewable Energy Sources")
|
| 226 |
correlation_matrix = data.corr()
|
| 227 |
-
fig_corr = px.imshow(correlation_matrix, title='Correlation Matrix of Renewable Capacity Factors', labels={'color': 'Correlation'})
|
| 228 |
-
st.plotly_chart(fig_corr)
|
| 229 |
|
| 230 |
# Note: You may need to adjust initial values or labels to fit your requirements.
|
|
|
|
| 6 |
import plotly.graph_objs as go
|
| 7 |
from plotly.subplots import make_subplots
|
| 8 |
import plotly.express as px
|
| 9 |
+
import datetime
|
| 10 |
|
| 11 |
# Function to fetch renewable energy data
|
| 12 |
def get_renewable_energy_data(city_code):
|
|
|
|
| 55 |
- yearly_demand (float): The total yearly demand in TWh/year.
|
| 56 |
Returns:
|
| 57 |
- fig (Figure): A plotly figure showing the optimized energy dispatch and state of charge (SOC) over time.
|
| 58 |
+
- curtailment_values (list): List of curtailment values for visualization.
|
| 59 |
+
- price_per_hour (list): List of estimated electricity price per hour for visualization.
|
| 60 |
"""
|
| 61 |
data, error = get_renewable_energy_data(city_code)
|
| 62 |
if error:
|
| 63 |
st.error(error)
|
| 64 |
+
return None, None, None
|
| 65 |
|
| 66 |
# Convert capacity factor columns to numeric values, handling any errors and filling missing data.
|
| 67 |
for col in data.columns[1:]:
|
|
|
|
| 139 |
battery_discharge_values = [battery_discharge[t].varValue for t in time_steps]
|
| 140 |
battery_charge_values = [-battery_charge[t].varValue for t in time_steps]
|
| 141 |
SOC_values = [SOC[t].varValue for t in time_steps]
|
| 142 |
+
curtailment_values = [curtailment[(r, t)].varValue for r in regions for t in time_steps]
|
| 143 |
+
|
| 144 |
+
# Calculate price per hour based on demand and generation (simplified example).
|
| 145 |
+
price_per_hour = [100 + 0.05 * demand[t] for t in time_steps] # Base price + factor for demand.
|
| 146 |
|
| 147 |
# Normalize state of charge (SOC) to a percentage.
|
| 148 |
max_SOC = max(SOC_values)
|
| 149 |
SOC_normalized = [(soc / max_SOC) * 100 for soc in SOC_values] if max_SOC > 0 else [0] * len(SOC_values)
|
| 150 |
|
| 151 |
+
# Create a subplot with 3 rows for energy dispatch, state of charge (SOC), and electricity price.
|
| 152 |
+
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.1,
|
| 153 |
+
subplot_titles=("Power supply and demand", "State of charge (Battery)", "Electricity Price Over Time"))
|
| 154 |
|
| 155 |
# Plot energy dispatch in the first row.
|
| 156 |
fig.add_trace(go.Scatter(x=data['Time'], y=supply_solar, mode='lines', stackgroup='one', name='Solar', line=dict(color='#FFD700', width=0)), row=1, col=1) # Solar: Gold
|
|
|
|
| 160 |
fig.add_trace(go.Scatter(x=data['Time'], y=battery_discharge_values, mode='lines', stackgroup='one', name='Battery Discharge', fill='tonexty', line=dict(color='#6A3D9A', width=0)), row=1, col=1) # Battery Discharge: Brown
|
| 161 |
fig.add_trace(go.Scatter(x=data['Time'], y=battery_charge_values, mode='lines', stackgroup='two', name='Battery Charge', fill='tonexty', line=dict(color='#6A3D9A', width=0)), row=1, col=1) # Battery Charge: Purple
|
| 162 |
fig.add_trace(go.Scatter(x=data['Time'], y=-demand, mode='lines', stackgroup='two', name='Demand', line=dict(color='black', width=0)), row=1, col=1) # Demand: Black
|
| 163 |
+
fig.add_trace(go.Scatter(x=data['Time'], y=curtailment_values, mode='lines', stackgroup='two', name='Curtailment', line=dict(color='#aaaaaa', width=0)), row=1, col=1) # Curtailment: Grey
|
| 164 |
|
| 165 |
# Plot state of charge (SOC) in the second row, normalized to 100%.
|
| 166 |
fig.add_trace(go.Scatter(x=data['Time'], y=SOC_normalized, mode='lines', name='State of Charge (SOC) - Normalized', line=dict(color='black')), row=2, col=1)
|
| 167 |
|
| 168 |
+
# Plot electricity price in the third row.
|
| 169 |
+
fig.add_trace(go.Scatter(x=data['Time'], y=price_per_hour, mode='lines', name='Electricity Price', line=dict(color='#FF4500')), row=3, col=1) # Price: Red-Orange
|
| 170 |
+
|
| 171 |
# Layout settings for the figure.
|
| 172 |
fig.update_layout(
|
| 173 |
title_text='Optimized result',
|
|
|
|
| 181 |
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 182 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 183 |
)
|
| 184 |
+
# Update the y-axis titles for each subplot.
|
| 185 |
fig.update_yaxes(title_text="State of Charge (%)", row=2, col=1)
|
| 186 |
+
fig.update_yaxes(title_text="Electricity Price (¥/MWh)", row=3, col=1)
|
| 187 |
|
| 188 |
+
return fig, curtailment_values, price_per_hour
|
| 189 |
|
| 190 |
# Streamlit UI for the application
|
| 191 |
+
st.set_page_config(page_title='Renewable Energy System Optimization', layout='wide')
|
| 192 |
st.title('Renewable Energy System Optimization')
|
| 193 |
|
| 194 |
# Explanation of the project
|
|
|
|
| 198 |
|
| 199 |
The optimization problem is solved using linear programming, ensuring a balance between supply and demand, and incorporating battery energy storage to manage intermittency issues inherent in renewable energy.
|
| 200 |
|
| 201 |
+
The visualizations provided help to better understand how different energy sources contribute to the overall power supply, how energy storage systems are utilized, and the impact of cost variations on energy prices.
|
| 202 |
""")
|
| 203 |
|
| 204 |
# Input fields for the user
|
| 205 |
+
with st.sidebar:
|
| 206 |
+
st.header('Input Parameters')
|
| 207 |
+
city_code = st.text_input("Enter City Code", value="")
|
| 208 |
+
solar_cost = st.number_input("Solar Capacity Cost (¥/MW)", value=80.0, help="Estimated average cost of solar capacity per MW")
|
| 209 |
+
onshore_wind_cost = st.number_input("Onshore Wind Capacity Cost (¥/MW)", value=120.0, help="Estimated average cost of onshore wind capacity per MW")
|
| 210 |
+
offshore_wind_cost = st.number_input("Offshore Wind Capacity Cost (¥/MW)", value=180.0, help="Estimated average cost of offshore wind capacity per MW")
|
| 211 |
+
river_cost = st.number_input("River Capacity Cost (¥/MW)", value=100.0, help="Estimated average cost of river (hydro) capacity per MW")
|
| 212 |
+
battery_cost = st.number_input("Battery Cost (¥/MWh)", value=80.0, help="Estimated average cost of battery storage per MWh")
|
| 213 |
+
yearly_demand = st.number_input("Yearly Power Demand (TWh/year)", value=15.0, help="Total yearly power demand in TWh")
|
| 214 |
|
| 215 |
# Button to trigger optimization
|
| 216 |
+
if st.button('Calculate Optimal Energy Mix'):
|
| 217 |
+
fig, curtailment_values, price_per_hour = optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wind_cost, river_cost, battery_cost, yearly_demand)
|
| 218 |
if fig:
|
| 219 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 220 |
|
| 221 |
# Additional analysis and visualizations
|
| 222 |
st.markdown("### Additional Analysis")
|
| 223 |
+
st.markdown("The following plots provide additional insights into the renewable energy mix, curtailment, and electricity price variations.")
|
| 224 |
|
| 225 |
# Data for additional visualizations
|
| 226 |
renewable_data = {
|
|
|
|
| 232 |
cost_df = pd.DataFrame(list(renewable_data.items()), columns=['Technology', 'Cost (¥/MW)'])
|
| 233 |
|
| 234 |
# Plot cost comparison
|
| 235 |
+
fig_cost = px.bar(cost_df, x='Technology', y='Cost (¥/MW)', color='Technology', title='Cost Comparison of Different Renewable Technologies', template='plotly_white')
|
| 236 |
+
st.plotly_chart(fig_cost, use_container_width=True)
|
| 237 |
+
|
| 238 |
+
# Plot curtailment over time
|
| 239 |
+
curtailment_df = pd.DataFrame({"Time": data['Time'], "Curtailment (MW)": curtailment_values})
|
| 240 |
+
fig_curtailment = px.line(curtailment_df, x='Time', y='Curtailment (MW)', title='Curtailment Over Time', template='plotly_white')
|
| 241 |
+
st.plotly_chart(fig_curtailment, use_container_width=True)
|
| 242 |
+
|
| 243 |
+
# Plot electricity price over time
|
| 244 |
+
price_df = pd.DataFrame({"Time": data['Time'], "Electricity Price (¥/MWh)": price_per_hour})
|
| 245 |
+
fig_price = px.line(price_df, x='Time', y='Electricity Price (¥/MWh)', title='Electricity Price Variation Over Time', template='plotly_white')
|
| 246 |
+
st.plotly_chart(fig_price, use_container_width=True)
|
| 247 |
|
| 248 |
# Correlation analysis of renewable capacity factors
|
| 249 |
+
if city_code and data is not None:
|
| 250 |
st.markdown("### Correlation Between Renewable Energy Sources")
|
| 251 |
correlation_matrix = data.corr()
|
| 252 |
+
fig_corr = px.imshow(correlation_matrix, title='Correlation Matrix of Renewable Capacity Factors', labels={'color': 'Correlation'}, template='plotly_white')
|
| 253 |
+
st.plotly_chart(fig_corr, use_container_width=True)
|
| 254 |
|
| 255 |
# Note: You may need to adjust initial values or labels to fit your requirements.
|