|
| 1 | +# --- |
| 2 | +# jupyter: |
| 3 | +# jupytext: |
| 4 | +# text_representation: |
| 5 | +# extension: .py |
| 6 | +# format_name: percent |
| 7 | +# format_version: '1.3' |
| 8 | +# jupytext_version: 1.16.4 |
| 9 | +# kernelspec: |
| 10 | +# display_name: calliope_docs_build |
| 11 | +# language: python |
| 12 | +# name: calliope_docs_build |
| 13 | +# --- |
| 14 | + |
| 15 | +# %% [markdown] |
| 16 | +# # Running models in different modes |
| 17 | +# |
| 18 | +# Models can be built and solved in different modes: |
| 19 | + |
| 20 | +# - `plan` mode. |
| 21 | +# In `plan` mode, the user defines upper and lower boundaries for technology capacities and the model decides on an optimal system configuration. |
| 22 | +# In this configuration, the total cost of investing in technologies and then using them to meet demand in every _timestep_ (e.g., every hour) is as low as possible. |
| 23 | +# - `operate` mode. |
| 24 | +# In `operate` mode, all capacity constraints are fixed and the system is operated with a receding horizon control algorithm. |
| 25 | +# This is sometimes known as a `dispatch` model - we're only concerned with the _dispatch_ of technologies whose capacities are already fixed. |
| 26 | +# Optimisation is limited to a time horizon which |
| 27 | +# - `spores` mode. |
| 28 | +# `SPORES` refers to Spatially-explicit Practically Optimal REsultS. |
| 29 | +# This run mode allows a user to generate any number of alternative results which are within a certain range of the optimal cost. |
| 30 | + |
| 31 | +# In this notebook we will run the Calliope national scale example model in these three modes. |
| 32 | + |
| 33 | +# More detail on these modes is given in the [_advanced_ section of the Calliope documentation](https://calliope.readthedocs.io/en/latest/advanced/mode/). |
| 34 | + |
| 35 | +# %% |
| 36 | + |
| 37 | +import plotly.express as px |
| 38 | +import plotly.graph_objects as go |
| 39 | +import xarray as xr |
| 40 | + |
| 41 | +import calliope |
| 42 | + |
| 43 | +# We update logging to show a bit more information but to hide the solver output, which can be long. |
| 44 | +calliope.set_log_verbosity("INFO", include_solver_output=False) |
| 45 | + |
| 46 | +# %% [markdown] |
| 47 | +# ## Running in `plan` mode. |
| 48 | + |
| 49 | +# %% |
| 50 | +# We subset to the same time range as operate mode |
| 51 | +model_plan = calliope.examples.national_scale(time_subset=["2005-01-01", "2005-01-10"]) |
| 52 | +model_plan.build() |
| 53 | +model_plan.solve() |
| 54 | + |
| 55 | +# %% [markdown] |
| 56 | +# ## Running in `operate` mode. |
| 57 | + |
| 58 | +# %% |
| 59 | +model_operate = calliope.examples.national_scale(scenario="operate") |
| 60 | +model_operate.build() |
| 61 | +model_operate.solve() |
| 62 | + |
| 63 | +# %% [markdown] |
| 64 | +# Note how we have capacity variables as parameters in the inputs and only dispatch variables in the results |
| 65 | + |
| 66 | +# %% |
| 67 | +model_operate.inputs[["flow_cap", "storage_cap", "area_use"]] |
| 68 | + |
| 69 | +# %% |
| 70 | +model_operate.results |
| 71 | + |
| 72 | +# %% [markdown] |
| 73 | +# ## Running in `spores` mode. |
| 74 | + |
| 75 | +# %% |
| 76 | +# We subset to the same time range as operate/plan mode |
| 77 | +model_spores = calliope.examples.national_scale( |
| 78 | + scenario="spores", time_subset=["2005-01-01", "2005-01-10"] |
| 79 | +) |
| 80 | +model_spores.build() |
| 81 | +model_spores.solve() |
| 82 | + |
| 83 | +# %% [markdown] |
| 84 | +# Note how we have a new `spores` dimension in our results. |
| 85 | + |
| 86 | +# %% |
| 87 | +model_spores.results |
| 88 | + |
| 89 | +# %% [markdown] |
| 90 | +# We can track the SPORES scores used between iterations using the `spores_score_cumulative` result. |
| 91 | +# This scoring mechanism is based on increasing the score of any technology-node combination where the |
| 92 | + |
| 93 | +# %% |
| 94 | +# We do some prettification of the outputs |
| 95 | +model_spores.results.spores_score_cumulative.to_series().where( |
| 96 | + lambda x: x > 0 |
| 97 | +).dropna().unstack("spores") |
| 98 | + |
| 99 | +# %% [markdown] |
| 100 | +# ## Visualising results |
| 101 | +# |
| 102 | +# We can use [plotly](https://plotly.com/) to quickly examine our results. |
| 103 | +# These are just some examples of how to visualise Calliope data. |
| 104 | + |
| 105 | +# %% |
| 106 | +# We set the color mapping to use in all our plots by extracting the colors defined in the technology definitions of our model. |
| 107 | +# We also create some reusable plotting functions. |
| 108 | +colors = model_plan.inputs.color.to_series().to_dict() |
| 109 | + |
| 110 | + |
| 111 | +def plot_flows(results: xr.Dataset) -> go.Figure: |
| 112 | + df_electricity = ( |
| 113 | + (results.flow_out.fillna(0) - results.flow_in.fillna(0)) |
| 114 | + .sel(carriers="power") |
| 115 | + .sum("nodes") |
| 116 | + .to_series() |
| 117 | + .where(lambda x: x != 0) |
| 118 | + .dropna() |
| 119 | + .to_frame("Flow in/out (kWh)") |
| 120 | + .reset_index() |
| 121 | + ) |
| 122 | + df_electricity_demand = df_electricity[df_electricity.techs == "demand_power"] |
| 123 | + df_electricity_other = df_electricity[df_electricity.techs != "demand_power"] |
| 124 | + |
| 125 | + fig = px.bar( |
| 126 | + df_electricity_other, |
| 127 | + x="timesteps", |
| 128 | + y="Flow in/out (kWh)", |
| 129 | + color="techs", |
| 130 | + color_discrete_map=colors, |
| 131 | + ) |
| 132 | + fig.add_scatter( |
| 133 | + x=df_electricity_demand.timesteps, |
| 134 | + y=-1 * df_electricity_demand["Flow in/out (kWh)"], |
| 135 | + marker_color="black", |
| 136 | + name="demand", |
| 137 | + ) |
| 138 | + return fig |
| 139 | + |
| 140 | + |
| 141 | +def plot_capacity(results: xr.Dataset, **plotly_kwargs) -> go.Figure: |
| 142 | + df_capacity = ( |
| 143 | + results.flow_cap.where(results.techs != "demand_power") |
| 144 | + .sel(carriers="power") |
| 145 | + .to_series() |
| 146 | + .where(lambda x: x != 0) |
| 147 | + .dropna() |
| 148 | + .to_frame("Flow capacity (kW)") |
| 149 | + .reset_index() |
| 150 | + ) |
| 151 | + |
| 152 | + fig = px.bar( |
| 153 | + df_capacity, |
| 154 | + x="nodes", |
| 155 | + y="Flow capacity (kW)", |
| 156 | + color="techs", |
| 157 | + color_discrete_map=colors, |
| 158 | + **plotly_kwargs, |
| 159 | + ) |
| 160 | + return fig |
| 161 | + |
| 162 | + |
| 163 | +# %% [markdown] |
| 164 | +# ### Using different `spores` scoring algorithms. |
| 165 | +# |
| 166 | +# We make a number of scoring algorithms accessible out-of-the-box, based on those we present in [Lombardi et al. (2023)](https://doi.org/10.1016/j.apenergy.2023.121002). |
| 167 | +# You can call them on solving the model. |
| 168 | +# Here, we'll compare the result on `flow_cap` from running each. |
| 169 | + |
| 170 | +# %% |
| 171 | +# We subset to the same time range as operate/plan mode |
| 172 | +model_spores = calliope.examples.national_scale( |
| 173 | + scenario="spores", time_subset=["2005-01-01", "2005-01-10"] |
| 174 | +) |
| 175 | +model_spores.build() |
| 176 | + |
| 177 | +spores_results = [] |
| 178 | +for algorithm in ["integer", "evolving_average", "random", "relative_deployment"]: |
| 179 | + model_spores.solve(**{"spores.scoring_algorithm": algorithm}, force=True) |
| 180 | + spores_results.append(model_spores.results.expand_dims(algorithm=[algorithm])) |
| 181 | + |
| 182 | +spores_results_da = xr.concat(spores_results, dim="algorithm") |
| 183 | + |
| 184 | +spores_results_da.flow_cap.to_series().dropna().unstack("spores") |
| 185 | + |
| 186 | +# %% [markdown] |
| 187 | +# ## `plan` vs `operate` |
| 188 | +# Here, we compare flows over the 10 days. |
| 189 | +# Note how flows do not match as the rolling horizon makes it difficult to make the correct storage charge/discharge decisions. |
| 190 | + |
| 191 | +# %% |
| 192 | +fig_flows_plan = plot_flows( |
| 193 | + model_plan.results.sel(timesteps=model_operate.results.timesteps) |
| 194 | +) |
| 195 | +fig_flows_plan.update_layout(title="Plan mode flows") |
| 196 | + |
| 197 | + |
| 198 | +# %% |
| 199 | +fig_flows_operate = plot_flows(model_operate.results) |
| 200 | +fig_flows_operate.update_layout(title="Operate mode flows") |
| 201 | + |
| 202 | +# %% [markdown] |
| 203 | +# ## `plan` vs `spores` |
| 204 | +# Here, we compare installed capacities between the baseline run (== `plan` mode) and the SPORES. |
| 205 | +# Note how the baseline SPORE is the same as `plan` mode and then results deviate considerably. |
| 206 | + |
| 207 | +# %% |
| 208 | +fig_flows_plan = plot_capacity(model_plan.results) |
| 209 | +fig_flows_plan.update_layout(title="Plan mode capacities") |
| 210 | + |
| 211 | +# %% |
| 212 | +fig_flows_spores = plot_capacity(model_spores.results, facet_col="spores") |
| 213 | +fig_flows_spores.update_layout(title="SPORES mode capacities") |
| 214 | + |
| 215 | +# %% [markdown] |
| 216 | +# ## Comparing `spores` scoring algorithms |
| 217 | +# Here, we compare installed capacities between the different SPORES runs. |
| 218 | + |
| 219 | +# %% |
| 220 | +fig_flows_spores = plot_capacity( |
| 221 | + spores_results_da, facet_col="spores", facet_row="algorithm" |
| 222 | +) |
| 223 | +fig_flows_spores.update_layout( |
| 224 | + title="SPORES mode capacities using different scoring algorithms", |
| 225 | + autosize=False, |
| 226 | + height=800, |
| 227 | +) |
0 commit comments