Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,11 +71,15 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 71 |
|
| 72 |
# Extract necessary data for optimization.
|
| 73 |
time_steps = range(len(data['Time']))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
solar_cf = data['solar hourly capacity factor']
|
| 75 |
-
onshore_wind_cf = data
|
| 76 |
-
offshore_wind_cf = data
|
| 77 |
-
river_cf = data
|
| 78 |
-
demand_cf = data
|
| 79 |
|
| 80 |
# Define regions and technologies.
|
| 81 |
regions = ['region1']
|
|
@@ -148,20 +152,25 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 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
|
| 152 |
-
", "Electricity Price Over Time"))
|
| 153 |
-
|
| 154 |
-
# Create separate figure for power supply and demand
|
| 155 |
fig_supply_demand = go.Figure()
|
| 156 |
-
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_solar, mode='lines', stackgroup='one', name='Solar'
|
| 157 |
-
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_onshore_wind, mode='lines', stackgroup='one', name='Onshore Wind'
|
| 158 |
-
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_offshore_wind, mode='lines', stackgroup='one', name='Offshore Wind'
|
| 159 |
-
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_river, mode='lines', stackgroup='one', name='
|
| 160 |
-
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=battery_discharge_values, mode='lines',
|
| 161 |
-
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=battery_charge_values, mode='lines',
|
| 162 |
-
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=-demand, mode='lines',
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
fig_supply_demand.update_layout(
|
| 166 |
title_text='Power Supply and Demand',
|
| 167 |
yaxis_title='Power dispatch (MW)',
|
|
@@ -172,12 +181,8 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 172 |
plot_bgcolor='white',
|
| 173 |
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 174 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 175 |
-
)
|
| 176 |
|
| 177 |
-
# Create separate figure for state of charge (SOC)
|
| 178 |
-
fig_soc = go.Figure()
|
| 179 |
-
fig_soc.add_trace(go.Scatter(x=data['Time'], y=SOC_normalized, mode='lines', name='State of Charge (SOC) - Normalized', line=dict(color='black')))
|
| 180 |
-
|
| 181 |
fig_soc.update_layout(
|
| 182 |
title_text='State of Charge (Battery)',
|
| 183 |
yaxis_title='State of Charge (%)',
|
|
@@ -189,27 +194,9 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 189 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 190 |
)
|
| 191 |
|
| 192 |
-
# Create separate figure for electricity price over time
|
| 193 |
-
fig_price = go.Figure()
|
| 194 |
-
fig_price.add_trace(go.Scatter(x=data['Time'], y=price_per_hour, mode='lines', name='Electricity Price', line=dict(color='#FF4500')))
|
| 195 |
-
|
| 196 |
fig_price.update_layout(
|
| 197 |
title_text='Electricity Price Over Time',
|
| 198 |
-
yaxis_title='Electricity Price (
|
| 199 |
-
font=dict(size=12),
|
| 200 |
-
margin=dict(l=40, r=40, t=40, b=40),
|
| 201 |
-
hovermode='x unified',
|
| 202 |
-
plot_bgcolor='white',
|
| 203 |
-
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 204 |
-
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 205 |
-
) # Price: Red-Orange
|
| 206 |
-
|
| 207 |
-
# Layout settings for the figure.
|
| 208 |
-
fig.update_layout(
|
| 209 |
-
title_text='Optimized result',
|
| 210 |
-
title_x=0.5,
|
| 211 |
-
yaxis_title='Power dispatch (MW)',
|
| 212 |
-
legend_title='Source',
|
| 213 |
font=dict(size=12),
|
| 214 |
margin=dict(l=40, r=40, t=40, b=40),
|
| 215 |
hovermode='x unified',
|
|
@@ -217,11 +204,8 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 217 |
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 218 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 219 |
)
|
| 220 |
-
# Update the y-axis titles for each subplot.
|
| 221 |
-
fig.update_yaxes(title_text="State of Charge (%)", row=2, col=1)
|
| 222 |
-
fig.update_yaxes(title_text="Electricity Price (¥/MWh)", row=3, col=1)
|
| 223 |
|
| 224 |
-
return
|
| 225 |
|
| 226 |
# Streamlit UI for the application
|
| 227 |
st.set_page_config(page_title='Renewable Energy System Optimization', layout='wide')
|
|
@@ -241,56 +225,19 @@ The visualizations provided help to better understand how different energy sourc
|
|
| 241 |
with st.sidebar:
|
| 242 |
st.header('Input Parameters')
|
| 243 |
city_code = st.text_input("Enter City Code", value="")
|
| 244 |
-
solar_cost = st.number_input("Solar Capacity Cost (
|
| 245 |
-
onshore_wind_cost = st.number_input("Onshore Wind Capacity Cost (
|
| 246 |
-
offshore_wind_cost = st.number_input("Offshore Wind Capacity Cost (
|
| 247 |
-
river_cost = st.number_input("River Capacity Cost (
|
| 248 |
-
battery_cost = st.number_input("Battery Cost (
|
| 249 |
yearly_demand = st.number_input("Yearly Power Demand (TWh/year)", value=15.0, help="Total yearly power demand in TWh")
|
| 250 |
|
| 251 |
# Button to trigger optimization
|
| 252 |
if st.button('Calculate Optimal Energy Mix'):
|
| 253 |
-
|
| 254 |
-
if
|
| 255 |
st.plotly_chart(fig_supply_demand, use_container_width=True, height=800)
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
# Additional analysis and visualizations
|
| 260 |
-
st.markdown("### Additional Analysis")
|
| 261 |
-
st.markdown("The following plots provide additional insights into the renewable energy mix, curtailment, and electricity price variations.")
|
| 262 |
-
|
| 263 |
-
# Data for additional visualizations
|
| 264 |
-
renewable_data = {
|
| 265 |
-
'Solar': solar_cost,
|
| 266 |
-
'Onshore Wind': onshore_wind_cost,
|
| 267 |
-
'Offshore Wind': offshore_wind_cost,
|
| 268 |
-
'Run of River': river_cost
|
| 269 |
-
}
|
| 270 |
-
cost_df = pd.DataFrame(list(renewable_data.items()), columns=['Technology', 'Cost (¥/MW)'])
|
| 271 |
-
|
| 272 |
-
# Plot cost comparison
|
| 273 |
-
fig_cost = px.bar(cost_df, x='Technology', y='Cost (¥/MW)', color='Technology', title='Cost Comparison of Different Renewable Technologies', template='plotly_white')
|
| 274 |
-
st.plotly_chart(fig_cost, use_container_width=True, height=800)
|
| 275 |
-
|
| 276 |
-
# Plot curtailment over time
|
| 277 |
-
curtailment_df = pd.DataFrame({"Time": fig.data[0].x, "Curtailment (MW)": curtailment_values})
|
| 278 |
-
fig_curtailment = px.line(curtailment_df, x='Time', y='Curtailment (MW)', title='Curtailment Over Time', template='plotly_white')
|
| 279 |
-
st.plotly_chart(fig_curtailment, use_container_width=True, height=800)
|
| 280 |
-
|
| 281 |
-
# Plot electricity price over time
|
| 282 |
-
price_df = pd.DataFrame({"Time": fig.data[0].x, "Electricity Price (¥/MWh)": price_per_hour})
|
| 283 |
-
fig_price = px.line(price_df, x='Time', y='Electricity Price (¥/MWh)', title='Electricity Price Variation Over Time', template='plotly_white')
|
| 284 |
st.plotly_chart(fig_price, use_container_width=True, height=800)
|
| 285 |
-
|
| 286 |
-
# Correlation analysis of renewable capacity factors
|
| 287 |
-
if city_code:
|
| 288 |
-
st.markdown("### Correlation Between Renewable Energy Sources")
|
| 289 |
-
correlation_matrix = get_renewable_energy_data(city_code)[0].corr()
|
| 290 |
-
if correlation_matrix is not None:
|
| 291 |
-
fig_corr = px.imshow(correlation_matrix, title='Correlation Matrix of Renewable Capacity Factors', labels={'color': 'Correlation'}, template='plotly_white')
|
| 292 |
-
st.plotly_chart(fig_corr, use_container_width=True, height=800)
|
| 293 |
-
fig_corr = px.imshow(correlation_matrix, title='Correlation Matrix of Renewable Capacity Factors', labels={'color': 'Correlation'}, template='plotly_white')
|
| 294 |
-
st.plotly_chart(fig_corr, use_container_width=True)
|
| 295 |
-
|
| 296 |
-
# Note: You may need to adjust initial values or labels to fit your requirements.
|
|
|
|
| 71 |
|
| 72 |
# Extract necessary data for optimization.
|
| 73 |
time_steps = range(len(data['Time']))
|
| 74 |
+
if 'solar hourly capacity factor' not in data.columns:
|
| 75 |
+
st.error("Solar data is missing in the retrieved dataset.")
|
| 76 |
+
return None, None, None
|
| 77 |
+
|
| 78 |
solar_cf = data['solar hourly capacity factor']
|
| 79 |
+
onshore_wind_cf = data.get('onshore_wind hourly capacity factor', pd.Series([0]*len(data)))
|
| 80 |
+
offshore_wind_cf = data.get('offshore_wind hourly capacity factor', pd.Series([0]*len(data)))
|
| 81 |
+
river_cf = data.get('river hourly capacity factor', pd.Series([0]*len(data)))
|
| 82 |
+
demand_cf = data.get('demand hourly capacity factor', pd.Series([0]*len(data)))
|
| 83 |
|
| 84 |
# Define regions and technologies.
|
| 85 |
regions = ['region1']
|
|
|
|
| 152 |
max_SOC = max(SOC_values)
|
| 153 |
SOC_normalized = [(soc / max_SOC) * 100 for soc in SOC_values] if max_SOC > 0 else [0] * len(SOC_values)
|
| 154 |
|
| 155 |
+
# Create figure for power supply and demand
|
|
|
|
|
|
|
|
|
|
| 156 |
fig_supply_demand = go.Figure()
|
| 157 |
+
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_solar, mode='lines', stackgroup='one', name='Solar'))
|
| 158 |
+
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_onshore_wind, mode='lines', stackgroup='one', name='Onshore Wind'))
|
| 159 |
+
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_offshore_wind, mode='lines', stackgroup='one', name='Offshore Wind'))
|
| 160 |
+
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=supply_river, mode='lines', stackgroup='one', name='River'))
|
| 161 |
+
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=battery_discharge_values, mode='lines', name='Battery Discharge', line=dict(color='red')))
|
| 162 |
+
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=battery_charge_values, mode='lines', name='Battery Charge', line=dict(color='blue')))
|
| 163 |
+
fig_supply_demand.add_trace(go.Scatter(x=data['Time'], y=-demand, mode='lines', name='Demand', line=dict(color='black')))
|
| 164 |
+
|
| 165 |
+
# Create figure for state of charge (SOC)
|
| 166 |
+
fig_soc = go.Figure()
|
| 167 |
+
fig_soc.add_trace(go.Scatter(x=data['Time'], y=SOC_normalized, mode='lines', name='State of Charge (SOC)'))
|
| 168 |
+
|
| 169 |
+
# Create figure for electricity price over time
|
| 170 |
+
fig_price = go.Figure()
|
| 171 |
+
fig_price.add_trace(go.Scatter(x=data['Time'], y=price_per_hour, mode='lines', name='Electricity Price'))
|
| 172 |
+
|
| 173 |
+
# Layout settings for the figures
|
| 174 |
fig_supply_demand.update_layout(
|
| 175 |
title_text='Power Supply and Demand',
|
| 176 |
yaxis_title='Power dispatch (MW)',
|
|
|
|
| 181 |
plot_bgcolor='white',
|
| 182 |
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 183 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 184 |
+
)
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
fig_soc.update_layout(
|
| 187 |
title_text='State of Charge (Battery)',
|
| 188 |
yaxis_title='State of Charge (%)',
|
|
|
|
| 194 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 195 |
)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
fig_price.update_layout(
|
| 198 |
title_text='Electricity Price Over Time',
|
| 199 |
+
yaxis_title='Electricity Price (\u00a5/MWh)',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
font=dict(size=12),
|
| 201 |
margin=dict(l=40, r=40, t=40, b=40),
|
| 202 |
hovermode='x unified',
|
|
|
|
| 204 |
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 205 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 206 |
)
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
return fig_supply_demand, fig_soc, fig_price, curtailment_values, price_per_hour
|
| 209 |
|
| 210 |
# Streamlit UI for the application
|
| 211 |
st.set_page_config(page_title='Renewable Energy System Optimization', layout='wide')
|
|
|
|
| 225 |
with st.sidebar:
|
| 226 |
st.header('Input Parameters')
|
| 227 |
city_code = st.text_input("Enter City Code", value="")
|
| 228 |
+
solar_cost = st.number_input("Solar Capacity Cost (\u00a5/MW)", value=80.0, help="Estimated average cost of solar capacity per MW")
|
| 229 |
+
onshore_wind_cost = st.number_input("Onshore Wind Capacity Cost (\u00a5/MW)", value=120.0, help="Estimated average cost of onshore wind capacity per MW")
|
| 230 |
+
offshore_wind_cost = st.number_input("Offshore Wind Capacity Cost (\u00a5/MW)", value=180.0, help="Estimated average cost of offshore wind capacity per MW")
|
| 231 |
+
river_cost = st.number_input("River Capacity Cost (\u00a5/MW)", value=100.0, help="Estimated average cost of river (hydro) capacity per MW")
|
| 232 |
+
battery_cost = st.number_input("Battery Cost (\u00a5/MWh)", value=80.0, help="Estimated average cost of battery storage per MWh")
|
| 233 |
yearly_demand = st.number_input("Yearly Power Demand (TWh/year)", value=15.0, help="Total yearly power demand in TWh")
|
| 234 |
|
| 235 |
# Button to trigger optimization
|
| 236 |
if st.button('Calculate Optimal Energy Mix'):
|
| 237 |
+
fig_supply_demand, fig_soc, fig_price, curtailment_values, price_per_hour = optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wind_cost, river_cost, battery_cost, yearly_demand)
|
| 238 |
+
if fig_supply_demand:
|
| 239 |
st.plotly_chart(fig_supply_demand, use_container_width=True, height=800)
|
| 240 |
+
if fig_soc:
|
| 241 |
+
st.plotly_chart(fig_soc, use_container_width=True, height=800)
|
| 242 |
+
if fig_price:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
st.plotly_chart(fig_price, use_container_width=True, height=800)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|