Running the urban scale example model¶
This notebook will show you how to load, build, solve, and examine the results of the urban scale example model.
import pandas as pd
import plotly.express as px
import calliope
# We increase logging verbosity
calliope.set_log_verbosity("INFO", include_solver_output=False)
Load model and examine inputs¶
model = calliope.examples.urban_scale()
[2025-03-14 18:57:28] INFO Model: initialising
[2025-03-14 18:57:28] INFO Model: preprocessing stage 1 (model_run)
[2025-03-14 18:57:30] INFO Model: preprocessing stage 2 (model_data)
[2025-03-14 18:57:30] INFO Model: preprocessing complete
Model inputs can be viewed at model.inputs
.
Variables are indexed over any combination of techs
, nodes
, carriers
, costs
and timesteps
.
model.inputs
<xarray.Dataset> Size: 88kB Dimensions: (costs: 1, techs: 12, carriers: 3, nodes: 4, timesteps: 48) Coordinates: * costs (costs) object 8B 'monetary' * techs (techs) object 96B 'N1_to_X2' ... 'supply_gri... * carriers (carriers) object 24B 'electricity' 'gas' 'heat' * nodes (nodes) object 32B 'N1' 'X1' 'X2' 'X3' * timesteps (timesteps) datetime64[ns] 384B 2005-07-01 ..... Data variables: (12/34) cost_interest_rate (costs) float64 8B 0.1 bigM float64 8B 1e+06 objective_cost_weights (costs) float64 8B 1.0 base_tech (techs) object 96B 'transmission' ... 'supply' carrier_export (techs, carriers) float64 288B nan nan ... nan carrier_in (nodes, techs, carriers) float64 1kB nan ... nan ... ... longitude (nodes) float64 32B -0.1247 -0.1613 ... -0.1311 source_use_equals (techs, timesteps) float64 5kB nan nan ... nan sink_use_equals (timesteps, techs, nodes) float64 18kB nan ..... definition_matrix (nodes, techs, carriers) bool 144B False ... ... timestep_resolution (timesteps) float64 384B 1.0 1.0 1.0 ... 1.0 1.0 timestep_weights (timesteps) float64 384B 1.0 1.0 1.0 ... 1.0 1.0 Attributes: calliope_version_defined: 0.7.0 calliope_version_initialised: 0.7.0.dev5 applied_overrides: scenario: None defaults: {'bigM': 1000000000.0, 'objective_cost_wei... allow_operate_mode: 1 name: Urban-scale example model timestamp_model_creation: 1741978648.097295
Individual data variables can be accessed easily, to_series().dropna()
allows us to view the data in a nice tabular format.
model.inputs.flow_cap_max.to_series().dropna()
techs carriers nodes N1_to_X2 heat N1 2000.0 X2 2000.0 N1_to_X3 heat N1 2000.0 X3 2000.0 X1_to_N1 heat N1 2000.0 X1 2000.0 X1_to_X2 electricity X1 2000.0 X2 2000.0 X1_to_X3 electricity X1 2000.0 X3 2000.0 boiler heat X2 600.0 X3 600.0 chp electricity X1 1500.0 pv electricity X1 250.0 X2 250.0 X3 50.0 supply_gas gas X1 2000.0 X2 2000.0 X3 2000.0 supply_grid_power electricity X1 2000.0 Name: flow_cap_max, dtype: float64
You can apply node/tech/carrier/timesteps only operations, like summing information over timesteps
model.inputs.sink_use_equals.sum(
"timesteps", min_count=1, skipna=True
).to_series().dropna()
techs nodes demand_electricity X1 35.271156 X2 8796.878622 X3 1244.604116 demand_heat X1 33.999992 X2 7147.808356 X3 50.567751 Name: sink_use_equals, dtype: float64
Build and solve the optimisation problem.¶
Results are loaded into model.results
.
By setting the log verbosity at the start of this tutorial to "INFO", we can see the timing of parts of the run, as well as the solver's log.
model.build()
model.solve()
[2025-03-14 18:57:30] INFO Model: backend build starting
[2025-03-14 18:57:30] INFO Math preprocessing | added file 'plan'.
[2025-03-14 18:57:30] INFO Math preprocessing | added file 'additional_math.yaml'.
[2025-03-14 18:57:30] INFO Math preprocessing | validated math against schema.
[2025-03-14 18:57:30] INFO Optimisation Model | parameters | Generated.
[2025-03-14 18:57:32] INFO Optimisation Model | Validated math strings.
[2025-03-14 18:57:32] INFO Optimisation Model | variables | Generated.
[2025-03-14 18:57:33] INFO Optimisation Model | global_expressions | Generated.
[2025-03-14 18:57:34] INFO Optimisation Model | constraints | Generated.
[2025-03-14 18:57:34] INFO Optimisation Model | piecewise_constraints | Generated.
[2025-03-14 18:57:34] INFO Optimisation Model | objectives | Generated.
[2025-03-14 18:57:34] INFO Model: backend build complete
[2025-03-14 18:57:34] INFO Optimisation model | starting model in plan mode.
[2025-03-14 18:57:34] INFO Backend: solver finished running. Time since start of solving optimisation problem: 0:00:00.225872
[2025-03-14 18:57:34] INFO Postprocessing: zero threshold of 1e-10 not required
[2025-03-14 18:57:34] INFO Postprocessing: ended. Time since start of solving optimisation problem: 0:00:00.315231
[2025-03-14 18:57:34] INFO Backend: model solve completed. Time since start of solving optimisation problem: 0:00:00.317619
Model results are held in the same structure as model inputs. The results consist of the optimal values for all decision variables, including capacities and carrier flow. There are also results, like system capacity factor and levelised costs, which are calculated in postprocessing before being added to the results Dataset
Examine results¶
model.results
<xarray.Dataset> Size: 379kB Dimensions: (nodes: 4, techs: 12, carriers: 3, timesteps: 48, costs: 1) Coordinates: * techs (techs) object 96B 'N1_to_X2' ... 'supply_gri... * carriers (carriers) <U11 132B 'electricity' 'gas' 'heat' * nodes (nodes) object 32B 'N1' 'X1' 'X2' 'X3' * timesteps (timesteps) datetime64[ns] 384B 2005-07-01 ..... * costs (costs) object 8B 'monetary' Data variables: (12/21) flow_cap (nodes, techs, carriers) float64 1kB nan ... nan link_flow_cap (techs) float64 96B 190.2 10.38 ... nan nan flow_out (nodes, techs, carriers, timesteps) float64 55kB ... flow_in (nodes, techs, carriers, timesteps) float64 55kB ... flow_export (nodes, techs, carriers, timesteps) float64 55kB ... area_use (nodes, techs) float64 384B nan nan ... nan nan ... ... cost_operation_fixed (nodes, techs, costs) float64 384B nan ... nan cost (nodes, techs, costs) float64 384B 0.05168 ..... capacity_factor (nodes, techs, carriers, timesteps) float64 55kB ... systemwide_capacity_factor (techs, carriers) float64 288B 0.0 0.0 ... 0.0 systemwide_levelised_cost (carriers, techs, costs) float64 288B nan ...... total_levelised_cost (carriers, costs) float64 24B 0.08069 ... 0.0... Attributes: (12/13) termination_condition: optimal calliope_version_defined: 0.7.0 calliope_version_initialised: 0.7.0.dev5 applied_overrides: scenario: None defaults: {'bigM': 1000000000.0, 'objective_cost_wei... ... ... name: Urban-scale example model timestamp_model_creation: 1741978648.097295 timestamp_build_start: 1741978650.08519 timestamp_build_complete: 1741978654.624815 timestamp_solve_start: 1741978654.626186 timestamp_solve_complete: 1741978654.943805
We can sum heat output over all locations and turn the result into a pandas DataFrame.
Note: heat output of transmission technologies (e.g., N1_to_X2
) is the import of heat at nodes.
df_heat = (
model.results.flow_out.sel(carriers="heat")
.sum("nodes", min_count=1, skipna=True)
.to_series()
.dropna()
.unstack("techs")
)
df_heat.head()
techs | N1_to_X2 | N1_to_X3 | X1_to_N1 | boiler | chp |
---|---|---|---|---|---|
timesteps | |||||
2005-07-01 00:00:00 | 242.341998 | 18.751161 | 85.869798 | 0.000000 | 92.861360 |
2005-07-01 01:00:00 | 66.368993 | 0.844722 | 72.541074 | 4.100046 | 78.466297 |
2005-07-01 02:00:00 | 67.582474 | 0.000000 | 72.915564 | 9.626102 | 78.876806 |
2005-07-01 03:00:00 | 67.487047 | 0.000000 | 72.812606 | 37.084989 | 78.877367 |
2005-07-01 04:00:00 | 70.052981 | 0.844807 | 76.515868 | 53.191064 | 83.204646 |
We can also examine total technology costs.
costs = model.results.cost.to_series().dropna()
costs.head()
nodes techs costs N1 N1_to_X2 monetary 0.051679 N1_to_X3 monetary 0.003761 X1_to_N1 monetary 0.154602 X1 X1_to_N1 monetary 0.154602 X1_to_X2 monetary 0.008286 Name: cost, dtype: float64
We can also examine levelized costs for each location and technology, which is calculated in a post-processing step.
lcoes = (
model.results.systemwide_levelised_cost.sel(carriers="electricity")
.to_series()
.dropna()
)
lcoes.head()
techs costs X1_to_X2 monetary 0.000002 X1_to_X3 monetary 0.000002 chp monetary 0.016822 pv monetary 0.044896 supply_grid_power monetary 0.115972 Name: systemwide_levelised_cost, dtype: float64
# We set the color mapping to use in all our plots by extracting the colors defined in the technology definitions of our model.
colors = model.inputs.color.to_series().to_dict()
Plotting flows¶
We do this by combinging in- and out-flows and separating demand from other technologies.
First, we look at the aggregated result across all nodes for electricity
, then we look at each node and carrier separately.
df_electricity = (
(model.results.flow_out.fillna(0) - model.results.flow_in.fillna(0))
.sel(carriers="electricity")
.sum("nodes")
.to_series()
.where(lambda x: x != 0)
.dropna()
.to_frame("Flow in/out (kWh)")
.reset_index()
)
df_electricity_demand = df_electricity[df_electricity.techs == "demand_electricity"]
df_electricity_other = df_electricity[df_electricity.techs != "demand_electricity"]
print(df_electricity.head())
fig1 = px.bar(
df_electricity_other,
x="timesteps",
y="Flow in/out (kWh)",
color="techs",
color_discrete_map=colors,
)
fig1.add_scatter(
x=df_electricity_demand.timesteps,
y=-1 * df_electricity_demand["Flow in/out (kWh)"],
marker_color="black",
name="demand",
)
techs timesteps Flow in/out (kWh) 0 X1_to_X2 2005-07-01 00:00:00 -1.929506 1 X1_to_X2 2005-07-01 01:00:00 -1.570625 2 X1_to_X2 2005-07-01 02:00:00 -1.581138 3 X1_to_X2 2005-07-01 03:00:00 -1.581138 4 X1_to_X2 2005-07-01 04:00:00 -1.688397