Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,6 +40,20 @@ def get_renewable_energy_data(city_code):
|
|
| 40 |
|
| 41 |
return result_df, None
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# Function to optimize the energy system
|
| 44 |
def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wind_cost, river_cost, battery_cost, yearly_demand):
|
| 45 |
"""
|
|
@@ -173,40 +187,6 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 173 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 174 |
)
|
| 175 |
|
| 176 |
-
# Plot state of charge (SOC).
|
| 177 |
-
fig_soc = go.Figure()
|
| 178 |
-
fig_soc.add_trace(go.Scatter(x=data['Time'], y=SOC_normalized, mode='lines', name='State of Charge (SOC) - Normalized', line=dict(color='black')))
|
| 179 |
-
|
| 180 |
-
# Layout settings for SOC plot.
|
| 181 |
-
fig_soc.update_layout(
|
| 182 |
-
title_text='State of Charge (Battery)',
|
| 183 |
-
title_x=0.5,
|
| 184 |
-
yaxis_title='State of Charge (%)',
|
| 185 |
-
font=dict(size=12),
|
| 186 |
-
margin=dict(l=40, r=40, t=40, b=40),
|
| 187 |
-
hovermode='x unified',
|
| 188 |
-
plot_bgcolor='white',
|
| 189 |
-
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 190 |
-
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 191 |
-
)
|
| 192 |
-
|
| 193 |
-
# Plot electricity price.
|
| 194 |
-
fig_price = go.Figure()
|
| 195 |
-
fig_price.add_trace(go.Scatter(x=data['Time'], y=price_per_hour, mode='lines', name='Electricity Price', line=dict(color='#FF4500'))) # Price: Red-Orange
|
| 196 |
-
|
| 197 |
-
# Layout settings for electricity price plot.
|
| 198 |
-
fig_price.update_layout(
|
| 199 |
-
title_text='Electricity Price Over Time',
|
| 200 |
-
title_x=0.5,
|
| 201 |
-
yaxis_title='Electricity Price (\u00a5/MWh)',
|
| 202 |
-
font=dict(size=12),
|
| 203 |
-
margin=dict(l=40, r=40, t=40, b=40),
|
| 204 |
-
hovermode='x unified',
|
| 205 |
-
plot_bgcolor='white',
|
| 206 |
-
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 207 |
-
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
# Return the figures and other outputs.
|
| 211 |
return fig_energy, curtailment_values, price_per_hour
|
| 212 |
|
|
@@ -218,9 +198,7 @@ st.title('Renewable Energy System Optimization')
|
|
| 218 |
st.markdown("""
|
| 219 |
### Overview
|
| 220 |
This application is designed to help researchers and policymakers explore and optimize renewable energy systems for a specified region. By inputting cost parameters for different renewable energy sources and energy storage systems, the application determines the optimal mix of resources to meet energy demand while minimizing cost.
|
| 221 |
-
|
| 222 |
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.
|
| 223 |
-
|
| 224 |
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.
|
| 225 |
""")
|
| 226 |
|
|
@@ -239,7 +217,7 @@ with st.sidebar:
|
|
| 239 |
if st.button('Calculate Optimal Energy Mix'):
|
| 240 |
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)
|
| 241 |
if fig:
|
| 242 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 243 |
|
| 244 |
# Additional analysis and visualizations
|
| 245 |
st.markdown("### Additional Analysis")
|
|
@@ -271,11 +249,17 @@ if st.button('Calculate Optimal Energy Mix'):
|
|
| 271 |
# Correlation analysis of renewable capacity factors
|
| 272 |
if city_code:
|
| 273 |
st.markdown("### Correlation Between Renewable Energy Sources")
|
| 274 |
-
|
| 275 |
-
if
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
return result_df, None
|
| 42 |
|
| 43 |
+
# Function to filter numeric columns
|
| 44 |
+
def filter_numeric_columns(df):
|
| 45 |
+
"""
|
| 46 |
+
Filters only numeric columns from the provided DataFrame.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
- df (DataFrame): The DataFrame from which to filter numeric columns.
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
- DataFrame: A DataFrame containing only numeric columns.
|
| 53 |
+
"""
|
| 54 |
+
numeric_df = df.select_dtypes(include=['number'])
|
| 55 |
+
return numeric_df
|
| 56 |
+
|
| 57 |
# Function to optimize the energy system
|
| 58 |
def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wind_cost, river_cost, battery_cost, yearly_demand):
|
| 59 |
"""
|
|
|
|
| 187 |
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 188 |
)
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
# Return the figures and other outputs.
|
| 191 |
return fig_energy, curtailment_values, price_per_hour
|
| 192 |
|
|
|
|
| 198 |
st.markdown("""
|
| 199 |
### Overview
|
| 200 |
This application is designed to help researchers and policymakers explore and optimize renewable energy systems for a specified region. By inputting cost parameters for different renewable energy sources and energy storage systems, the application determines the optimal mix of resources to meet energy demand while minimizing cost.
|
|
|
|
| 201 |
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.
|
|
|
|
| 202 |
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.
|
| 203 |
""")
|
| 204 |
|
|
|
|
| 217 |
if st.button('Calculate Optimal Energy Mix'):
|
| 218 |
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)
|
| 219 |
if fig:
|
| 220 |
+
st.plotly_chart(fig, use_container_width=True, height=800)
|
| 221 |
|
| 222 |
# Additional analysis and visualizations
|
| 223 |
st.markdown("### Additional Analysis")
|
|
|
|
| 249 |
# Correlation analysis of renewable capacity factors
|
| 250 |
if city_code:
|
| 251 |
st.markdown("### Correlation Between Renewable Energy Sources")
|
| 252 |
+
energy_data_df, error = get_renewable_energy_data(city_code)
|
| 253 |
+
if error:
|
| 254 |
+
st.error(error)
|
| 255 |
+
else:
|
| 256 |
+
numeric_energy_data_df = filter_numeric_columns(energy_data_df)
|
| 257 |
+
|
| 258 |
+
# Handling missing values by filling them with 0.
|
| 259 |
+
numeric_energy_data_df = numeric_energy_data_df.fillna(0)
|
| 260 |
+
|
| 261 |
+
correlation_matrix = numeric_energy_data_df.corr()
|
| 262 |
+
|
| 263 |
+
if correlation_matrix is not None:
|
| 264 |
+
fig_corr = px.imshow(correlation_matrix, title='Correlation Matrix of Renewable Capacity Factors', labels={'color': 'Correlation'}, template='plotly_white')
|
| 265 |
+
st.plotly_chart(fig_corr, use_container_width=True, height=800)
|