Running the national scale example model¶
This notebook will show you how to load, build, solve, and examine the results of the national 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.national_scale()
[2025-03-14 18:57:16] INFO Model: initialising
[2025-03-14 18:57:16] INFO Model: preprocessing stage 1 (model_run)
[2025-03-14 18:57:18] INFO Model: preprocessing stage 2 (model_data)
[2025-03-14 18:57:18] 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: 82kB Dimensions: (costs: 1, techs: 8, nodes: 5, carriers: 1, timesteps: 120) Coordinates: * costs (costs) object 8B 'monetary' * techs (techs) object 64B 'battery' ... 'region1_t... * carriers (carriers) object 8B 'power' * nodes (nodes) <U9 180B 'region1' ... 'region2' * timesteps (timesteps) datetime64[ns] 960B 2005-01-01 ... Data variables: (12/35) 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 64B 'storage' ... 'transmiss... carrier_in (nodes, techs, carriers) float64 320B nan .... color (techs) object 64B '#3B61E3' ... '#8465A9' ... ... sink_use_equals (timesteps, nodes, techs) float64 38kB nan ... source_use_max (timesteps, nodes, techs) float64 38kB nan ... definition_matrix (nodes, techs, carriers) bool 40B False ...... distance (techs) float64 64B nan nan ... 111.0 512.3 timestep_resolution (timesteps) float64 960B 1.0 1.0 ... 1.0 1.0 timestep_weights (timesteps) float64 960B 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: National-scale example model timestamp_model_creation: 1741978636.499071
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()
nodes techs region1 ccgt 30000.0 region1_to_region2 10000.0 region1_1 csp 10000.0 region1_2 csp 10000.0 region1_3 csp 10000.0 region2 battery 1000.0 region1_to_region2 10000.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()
nodes techs region1 demand_power 3793748.48 region2 demand_power 288114.75 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:18] INFO Model: backend build starting
[2025-03-14 18:57:18] INFO Math preprocessing | added file 'plan'.
[2025-03-14 18:57:18] INFO Math preprocessing | validated math against schema.
[2025-03-14 18:57:18] INFO Optimisation Model | parameters | Generated.
[2025-03-14 18:57:20] INFO Optimisation Model | Validated math strings.
[2025-03-14 18:57:21] INFO Optimisation Model | variables | Generated.
[2025-03-14 18:57:21] INFO Optimisation Model | global_expressions | Generated.
[2025-03-14 18:57:23] INFO Optimisation Model | constraints | Generated.
[2025-03-14 18:57:23] INFO Optimisation Model | piecewise_constraints | Generated.
[2025-03-14 18:57:23] INFO Optimisation Model | objectives | Generated.
[2025-03-14 18:57:23] INFO Model: backend build complete
[2025-03-14 18:57:23] INFO Optimisation model | starting model in plan mode.
[2025-03-14 18:57:23] INFO Backend: solver finished running. Time since start of solving optimisation problem: 0:00:00.366870
[2025-03-14 18:57:23] WARNING Postprocessing: All values < 1e-10 set to 0 in flow_out, flow_in, storage, flow_out_inc_eff, flow_in_inc_eff, capacity_factor
[2025-03-14 18:57:23] INFO Postprocessing: ended. Time since start of solving optimisation problem: 0:00:00.460292
[2025-03-14 18:57:23] INFO Backend: model solve completed. Time since start of solving optimisation problem: 0:00:00.462693
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: 317kB Dimensions: (nodes: 5, techs: 8, carriers: 1, timesteps: 120, costs: 1) Coordinates: * techs (techs) object 64B 'battery' ... 'region1_to... * carriers (carriers) <U5 20B 'power' * nodes (nodes) <U9 180B 'region1' ... 'region2' * timesteps (timesteps) datetime64[ns] 960B 2005-01-01 .... * costs (costs) object 8B 'monetary' Data variables: (12/24) flow_cap (nodes, techs, carriers) float64 320B nan ..... link_flow_cap (techs) float64 64B nan nan ... 3.23e+03 flow_out (nodes, techs, carriers, timesteps) float64 38kB ... flow_in (nodes, techs, carriers, timesteps) float64 38kB ... area_use (nodes, techs) float64 320B nan nan ... nan nan source_use (nodes, techs, timesteps) float64 38kB nan .... ... ... cost_investment_annualised (nodes, techs, costs) float64 320B nan ... 4... cost (nodes, techs, costs) float64 320B nan ... 1... capacity_factor (nodes, techs, carriers, timesteps) float64 38kB ... systemwide_capacity_factor (techs, carriers) float64 64B 0.1317 ... 0.3738 systemwide_levelised_cost (carriers, techs, costs) float64 64B 0.1 ...... total_levelised_cost (carriers, costs) float64 8B 0.06701 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: National-scale example model timestamp_model_creation: 1741978636.499071 timestamp_build_start: 1741978638.386217 timestamp_build_complete: 1741978643.435761 timestamp_solve_start: 1741978643.437133 timestamp_solve_complete: 1741978643.899826
We can sum electricity output over all locations and turn the result into a pandas DataFrame.
Note: electricity output of transmission technologies (e.g., region1_to_region2
) is the import of electricity at nodes.
df_electricity = model.results.flow_out.sel(carriers="power").sum("nodes").to_series()
df_electricity.head()
techs timesteps battery 2005-01-01 00:00:00 0.0 2005-01-01 01:00:00 0.0 2005-01-01 02:00:00 0.0 2005-01-01 03:00:00 0.0 2005-01-01 04:00:00 0.0 Name: flow_out, dtype: float64
We can also view total costs associated with each of our technologies at each of our nodes:
costs = model.results.cost.to_series().dropna()
costs.head()
nodes techs costs region1 ccgt monetary 170306.866481 region1_to_region2 monetary 487.527358 region1_1 csp monetary 94592.428366 region1_2 csp monetary 0.000000 region1_3 csp monetary 9013.121387 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.to_series().dropna()
lcoes
carriers techs costs power battery monetary 0.100035 ccgt monetary 0.049961 csp monetary 0.142719 region1_to_region2 monetary 0.005364 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, then we look at each node separately.
df_electricity = (
(model.results.flow_out.fillna(0) - model.results.flow_in.fillna(0))
.sel(carriers="power")
.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_power"]
df_electricity_other = df_electricity[df_electricity.techs != "demand_power"]
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 battery 2005-01-01 14:00:00 -76.402643 1 battery 2005-01-01 15:00:00 -286.886000 2 battery 2005-01-01 16:00:00 163.934000 3 battery 2005-01-01 17:00:00 122.950000 4 battery 2005-01-01 18:00:00 40.984000