Rung 9: a multi-link with four output ports — power and heat sold, waste heat vented, and a station service the link draws back¶
One rung of the PyPSA corpus: the file pypsa.yaml projected onto what this network builds, attached to that network, and held to what PyPSA solves it to.
✔ Verified against pypsa 1.3.0 — objective 11714.4 on both sides; structure ✔ 5 constraints · 2 variables, name for name; size ✔ 92 rows · ✔ 32 columns · ✔ 116 nonzeros; duals ✔ 92 rows; model for model: 8 blocks equal, 0 documented splits.
Rows and columns, PyPSA against specsolve, name for name
| row | PyPSA | specsolve |
|---|---|---|
Bus-nodal_balance |
28 | 28 |
Generator-fix-p-lower |
24 | 24 |
Generator-fix-p-upper |
24 | 24 |
Link-fix-p-lower |
8 | 8 |
Link-fix-p-upper |
8 | 8 |
| column | PyPSA | specsolve |
|---|---|---|
Generator-p |
24 | 24 |
Link-p |
8 | 8 |
The model¶
The same model, as math
The spec of the model a plain n.optimize() builds, in one file. Every declaration is named Component_attribute after the PyPSA statement it stands for, and each constraint's description opens with the linopy name PyPSA gives that row, so the two can be read side by side. PyPSA's regimes — extendable, committable — are data columns and become where: masks. Bounds are the explicit rows PyPSA writes, so their duals are row duals. Parameters no PyPSA table carries verbatim are computed in data prep and say so in their description.
Sets¶
| Symbol | Meaning |
|---|---|
| \(\mathcal{T}\) | index \(t\) — snapshot — dispatch periods |
| \(\mathcal{N}\) | index \(n\) — bus with \(\mathrm{Generator\_bus}: \mathcal{G} \to \mathcal{N},\ \mathrm{Link\_bus0}: \mathcal{L} \to \mathcal{N},\ \mathrm{Link\_output\_bus}: \mathcal{O} \to \mathcal{N},\ \mathrm{Load\_bus}: \mathcal{D} \to \mathcal{N}\) — network nodes |
| \(\mathcal{G}\) | index \(g\) — generator with \(\mathrm{Generator\_bus}: \mathcal{G} \to \mathcal{N}\) — generating units, each on one bus |
| \(\mathcal{L}\) | index \(l\) — link with \(\mathrm{Link\_bus0}: \mathcal{L} \to \mathcal{N},\ \mathrm{Link\_output\_link}: \mathcal{O} \to \mathcal{L}\) — controllable connections, each from one bus to the buses it delivers to |
| \(\mathcal{O}\) | index \(o\) — link_output with \(\mathrm{Link\_output\_link}: \mathcal{O} \to \mathcal{L},\ \mathrm{Link\_output\_bus}: \mathcal{O} \to \mathcal{N}\) — a link's output ports, one label per port a link declares — PyPSA's bus1, bus2, … columns read long, so a link of any number of output ports is one term in the balance, data prep |
| \(\mathcal{D}\) | index \(d\) — load with \(\mathrm{Load\_bus}: \mathcal{D} \to \mathcal{N}\) — demands, each on one bus |
Parameters¶
| Symbol | Meaning |
|---|---|
| \(\mathrm{w}\) | snapshot_weightings_objective over \(\mathcal{T}\) — PyPSA's snapshot_weightings.objective — hours a snapshot stands for in the cost |
| \(\mathrm{p}^{\mathrm{nom}}\) | Generator_p_nom over \(\mathcal{G}\) — nominal power |
| \(\mathrm{ext}\) | Generator_p_nom_extendable over \(\mathcal{G}\) — whether the nominal power is a decision |
| \(\underline{\mathrm{p}}\) | Generator_p_min_pu over \(\mathcal{T} \times \mathcal{G}\) — least output, per unit of nominal power |
| \(\overline{\mathrm{p}}\) | Generator_p_max_pu over \(\mathcal{T} \times \mathcal{G}\) — most output, per unit of nominal power — an availability profile |
| \(\mathrm{c}\) | Generator_marginal_cost over \(\mathcal{T} \times \mathcal{G}\) — cost of one unit of output |
| \(\mathrm{com}\) | Generator_committable over \(\mathcal{G}\) — whether output is gated by an on/off status decision |
| \(\mathrm{f}^{\mathrm{nom}}\) | Link_p_nom over \(\mathcal{L}\) — nominal power |
| \(\mathrm{ext}^{f}\) | Link_p_nom_extendable over \(\mathcal{L}\) — whether the nominal power is a decision |
| \(\underline{\mathrm{f}}\) | Link_p_min_pu over \(\mathcal{T} \times \mathcal{L}\) — least flow, per unit of nominal power — negative for a link that carries both ways |
| \(\overline{\mathrm{f}}\) | Link_p_max_pu over \(\mathcal{T} \times \mathcal{L}\) — most flow, per unit of nominal power |
| \(\eta\) | Link_efficiency over \(\mathcal{O}\) — share of the flow that arrives at an output port, PyPSA's efficiency, efficiency2, … read long — negative where that port consumes rather than delivers |
| \(\mathrm{d}^{f}\) | Link_output_delay over \(\mathcal{O}\) — snapshots a port's delivery lags its link's flow — PyPSA's delay, delay2, … read long, in snapshot_weightings.generators units, which the file states as whole snapshots; zero for a port that delivers at once |
| \(\mathrm{cyc}^{f}\) | Link_output_cyclic_delay over \(\mathcal{O}\) — whether a delayed port's flow wraps from the horizon's end — PyPSA's cyclic_delay, cyclic_delay2, …; where it does not, the flow still in transit at the first snapshots is lost |
| \(\mathrm{c}^{f}\) | Link_marginal_cost over \(\mathcal{T} \times \mathcal{L}\) — cost of one unit of flow |
| \(\mathrm{load}\) | Load_p_set over \(\mathcal{T} \times \mathcal{D}\) — demand |
Variables¶
| Symbol | Meaning |
|---|---|
| \(p\) | Generator_p over \(\mathcal{T} \times \mathcal{G}\) — Generator-p — output of a generator in a snapshot |
| \(f\) | Link_p over \(\mathcal{T} \times \mathcal{L}\) — Link-p — PyPSA's p0, the flow measured at the Link_bus0 end: a positive value withdraws there and injects at every bus the link's output ports deliver to |
Definitions¶
| Symbol | Meaning |
|---|---|
| \(\mathit{Link\_output\_arrival}\) | Link_output_arrival over \(\mathcal{T} \times \mathcal{O}\) — what a link delivers to an output port at a snapshot — its flow after the port's efficiency, delayed by the port's delay; where the port is cyclic_delay the delayed flow wraps from the horizon's end, and where it is not the flow still in transit at the first snapshots is lost. A port that does not delay (delay zero) delivers its flow unshifted, cyclic or not |
\(t \ominus k\) denotes cyclic translation: index \(t-k\) taken modulo the size of the dimension (roll). Plain \(t-k\) (shift) has no wraparound — terms translated past the edge are simply absent.
\(t \boxminus_{v} k\) denotes translation with \(v\) standing where index \(t-k\) leaves the dimension (shift(edge=v)), so the row at that boundary is built and carries \(v\) rather than being dropped.
Objective¶
Subject to¶
Generator_fix_p_lower
Generator_fix_p_upper
Link_fix_p_lower
Link_fix_p_upper
Bus_nodal_balance
Definitions¶
Link_output_arrival
Variable domains¶
Generator_p
Link_p
The spec, differential/pypsa/rungs/rung_09_multilink.yaml — the file projected onto what this rung builds:
description: The spec of the model a plain `n.optimize()` builds, in one file. Every declaration is named
`Component_attribute` after the PyPSA statement it stands for, and each constraint's description opens
with the linopy name PyPSA gives that row, so the two can be read side by side. PyPSA's regimes — extendable,
committable — are data columns and become `where:` masks. Bounds are the explicit rows PyPSA writes,
so their duals are row duals. Parameters no PyPSA table carries verbatim are computed in data prep and
say so in their description.
dimensions:
snapshot: {description: dispatch periods, dtype: datetime}
bus: {description: network nodes}
generator: {description: 'generating units, each on one bus'}
link: {description: 'controllable connections, each from one bus to the buses it delivers to'}
link_output: {description: 'a link''s output ports, one label per port a link declares — PyPSA''s `bus1`,
`bus2`, … columns read long, so a link of any number of output ports is one term in the balance,
data prep'}
load: {description: 'demands, each on one bus'}
relations:
Generator_bus: {description: the bus a generator sits on, key: generator, values: bus}
Link_bus0: {description: the bus a link leaves, key: link, values: bus}
Link_output_link: {description: the link an output port belongs to, key: link_output, values: link}
Link_output_bus: {description: 'the bus an output port delivers to — PyPSA''s `bus1`, `bus2`, … columns.
A link of three output ports is three labels here rather than a third relation, so the file states
any number of them', key: link_output, values: bus}
Load_bus: {description: the bus a load sits on, key: load, values: bus}
parameters:
snapshot_weightings_objective:
description: PyPSA's `snapshot_weightings.objective` — hours a snapshot stands for in the cost
dims: [snapshot]
Generator_p_nom:
description: nominal power
dims: [generator]
Generator_p_nom_extendable:
description: whether the nominal power is a decision
dims: [generator]
dtype: bool
Generator_p_min_pu:
description: least output, per unit of nominal power
dims: [snapshot, generator]
Generator_p_max_pu:
description: most output, per unit of nominal power — an availability profile
dims: [snapshot, generator]
Generator_marginal_cost:
description: cost of one unit of output
dims: [snapshot, generator]
Generator_committable:
description: whether output is gated by an on/off status decision
dims: [generator]
dtype: bool
Link_p_nom:
description: nominal power
dims: [link]
Link_p_nom_extendable:
description: whether the nominal power is a decision
dims: [link]
dtype: bool
Link_p_min_pu:
description: least flow, per unit of nominal power — negative for a link that carries both ways
dims: [snapshot, link]
Link_p_max_pu:
description: most flow, per unit of nominal power
dims: [snapshot, link]
Link_efficiency:
description: share of the flow that arrives at an output port, PyPSA's `efficiency`, `efficiency2`,
… read long — negative where that port consumes rather than delivers
dims: [link_output]
Link_output_delay:
description: snapshots a port's delivery lags its link's flow — PyPSA's `delay`, `delay2`, … read
long, in `snapshot_weightings.generators` units, which the file states as whole snapshots; zero
for a port that delivers at once
dims: [link_output]
dtype: int
Link_output_cyclic_delay:
description: whether a delayed port's flow wraps from the horizon's end — PyPSA's `cyclic_delay`,
`cyclic_delay2`, …; where it does not, the flow still in transit at the first snapshots is lost
dims: [link_output]
dtype: bool
Link_marginal_cost:
description: cost of one unit of flow
dims: [snapshot, link]
Load_p_set:
description: demand
dims: [snapshot, load]
variables:
Generator_p:
description: '`Generator-p` — output of a generator in a snapshot'
dims: [snapshot, generator]
Link_p:
description: '`Link-p` — PyPSA''s `p0`, the flow measured at the `Link_bus0` end: a positive value
withdraws there and injects at every bus the link''s output ports deliver to'
dims: [snapshot, link]
constraints:
Generator_fix_p_lower:
description: '`Generator-fix-p-lower` — a fixed generator outputs at least its minimum'
dims: [snapshot, generator]
where: not Generator_p_nom_extendable AND not Generator_committable
expression: Generator_p >= Generator_p_min_pu * Generator_p_nom
Generator_fix_p_upper:
description: '`Generator-fix-p-upper` — a fixed generator outputs at most what is available'
dims: [snapshot, generator]
where: not Generator_p_nom_extendable AND not Generator_committable
expression: Generator_p <= Generator_p_max_pu * Generator_p_nom
Link_fix_p_lower:
description: '`Link-fix-p-lower` — a fixed link carries at least its minimum, negative for the other
way'
dims: [snapshot, link]
where: not Link_p_nom_extendable
expression: Link_p >= Link_p_min_pu * Link_p_nom
Link_fix_p_upper:
description: '`Link-fix-p-upper` — a fixed link carries at most its nominal power'
dims: [snapshot, link]
where: not Link_p_nom_extendable
expression: Link_p <= Link_p_max_pu * Link_p_nom
Bus_nodal_balance:
description: '`Bus-nodal_balance` — what is generated at a bus, storage dispatch and stores included,
less what the links take away, plus what arrives over them after losses and any delay at every port
they deliver to, meets the load there. A bus nothing is attached to has no row; PyPSA refuses one
that carries load, and this file does not yet.'
dims: [snapshot, bus]
expression: sum(Generator_p, by=Generator_bus, over=generator, into=bus) - sum(Link_p, by=Link_bus0,
over=link, into=bus) + sum(Link_output_arrival, by=Link_output_bus, over=link_output, into=bus)
== sum(Load_p_set, by=Load_bus, over=load, into=bus)
expressions:
Link_output_arrival:
description: what a link delivers to an output port at a snapshot — its flow after the port's efficiency,
delayed by the port's `delay`; where the port is `cyclic_delay` the delayed flow wraps from the
horizon's end, and where it is not the flow still in transit at the first snapshots is lost. A port
that does not delay (`delay` zero) delivers its flow unshifted, cyclic or not
dims: [snapshot, link_output]
cases:
wrapping: {when: Link_output_cyclic_delay, expression: 'shift(at(Link_p, by=Link_output_link, over=link,
into=link_output) * Link_efficiency, along=snapshot, offset=Link_output_delay, edge=''wrap'')'}
otherwise: shift(at(Link_p, by=Link_output_link, over=link, into=link_output) * Link_efficiency, along=snapshot,
offset=Link_output_delay, edge=0)
objective: {sense: minimize, description: 'operating cost, each snapshot weighted by the hours it stands
for', expression: sum(Generator_p * Generator_marginal_cost * snapshot_weightings_objective) + sum(Link_p
* Link_marginal_cost * snapshot_weightings_objective)}
The prep — every table the spec declares, from the network — and the solve:
from differential.pypsa.prep import relation, static, varying, weighting
def _link_ports(n: pypsa.Network) -> pd.DataFrame:
"""A link's output ports read long — one row per port a link declares, carrying the link, the bus it delivers to and its efficiency.
PyPSA spells the ports across columns — ``bus1``/``efficiency``, ``bus2``/``efficiency2``, … — and a
link declares a port by naming a bus in one, so a link of any port count is as many rows here and
one term in the balance. The label is the link and the column the port came from.
"""
links = n.static('Link')
blank = pd.Series('', index=links.index, dtype=str)
frames = []
for port in ['1', *n.components.links.additional_ports]:
suffix = '' if port == '1' else port
buses = links.get(f'bus{port}', blank).astype(str)
# `efficiency`, `delay` and `cyclic_delay` are PyPSA's unsuffixed attributes: port 1
# spells them bare and every port after it takes the number
efficiencies = links.get(f'efficiency{suffix}', pd.Series(1.0, index=links.index)).astype(float)
delays = links.get(f'delay{suffix}', pd.Series(0, index=links.index)).fillna(0).astype(int)
cyclic = links.get(f'cyclic_delay{suffix}', pd.Series(False, index=links.index)).fillna(False).astype(bool)
frame = pd.DataFrame(
keyed(links.index, 'link')
| {
'bus': buses.to_numpy(),
'value': efficiencies.to_numpy(),
'delay': delays.to_numpy(),
'cyclic_delay': cyclic.to_numpy(),
'port': int(port),
}
)
frames.append(frame[buses.to_numpy() != ''])
ports = pd.concat(frames, ignore_index=True).sort_values(['link', 'port'], kind='stable')
ports['link_output'] = ports['link'] + '_bus' + ports['port'].astype(str)
return ports.drop(columns='port').reset_index(drop=True)
def _per_port(n: pypsa.Network, column: str, as_name: str | None = None) -> pd.DataFrame:
"""One column of the long port table keyed by ``link_output`` — what a port names, or what it carries.
*as_name* is what the file calls it: a relation keeps its target dimension's
own name, and every parameter over the ports lands under ``value``.
"""
ports = _link_ports(n)
keys = [key for key in ('scenario', 'link_output') if key in ports.columns]
return ports[[*keys, column]].rename(columns={column: as_name or column})
n = build() # the network from the PyPSA tab
sources = {
'snapshot': pl.Series('snapshot', list(timesteps(n)), dtype=pl.Datetime('us')),
'bus': pl.Series('bus', list(names(n.buses.index).astype(str)), dtype=pl.String),
'generator': pl.Series('generator', list(names(generators.index).astype(str)), dtype=pl.String),
'link': pl.Series('link', list(names(links.index).astype(str)), dtype=pl.String),
'link_output': pl.Series('link_output', list(pd.unique(_link_ports(n)['link_output'])), dtype=pl.String),
'load': pl.Series('load', list(names(loads.index).astype(str)), dtype=pl.String),
'Generator_bus': relation(n, 'Generator', 'bus'),
'Link_bus0': relation(n, 'Link', 'bus0'),
'Link_output_link': _per_port(n, 'link'),
'Link_output_bus': _per_port(n, 'bus'),
'Load_bus': relation(n, 'Load', 'bus'),
'snapshot_weightings_objective': weighting(n, 'objective'),
'Generator_p_nom': static(n, 'Generator', 'p_nom'),
'Generator_p_nom_extendable': static(n, 'Generator', 'p_nom_extendable'),
'Generator_p_min_pu': varying(n, 'Generator', 'p_min_pu'),
'Generator_p_max_pu': varying(n, 'Generator', 'p_max_pu'),
'Generator_marginal_cost': varying(n, 'Generator', 'marginal_cost'),
'Generator_committable': static(n, 'Generator', 'committable'),
'Link_p_nom': static(n, 'Link', 'p_nom'),
'Link_p_nom_extendable': static(n, 'Link', 'p_nom_extendable'),
'Link_p_min_pu': varying(n, 'Link', 'p_min_pu'),
'Link_p_max_pu': varying(n, 'Link', 'p_max_pu'),
'Link_efficiency': _per_port(n, 'value'),
'Link_output_delay': _per_port(n, 'delay', 'value'),
'Link_output_cyclic_delay': _per_port(n, 'cyclic_delay', 'value'),
'Link_marginal_cost': varying(n, 'Link', 'marginal_cost'),
'Load_p_set': varying(n, 'Load', 'p_set'),
}
with sps.solve('differential/pypsa/rungs/rung_09_multilink.yaml', sources) as solution:
solution.objective # 11714.4
The network, rung_09_multilink.py in the corpus — the spine plus what this rung adds:
"""Rung 9: a multi-link with four output ports — power and heat sold, waste heat vented, and a station service the link draws back."""
from __future__ import annotations
import spine
def build():
"""The spine plus this rung's additions, as a ``pypsa.Network``."""
n = spine.build()
n.add('Bus', 'gasb')
n.add('Bus', 'power')
n.add('Bus', 'heat')
n.add('Bus', 'flue')
n.add('Bus', 'aux')
n.add('Generator', 'well', bus='gasb', p_nom=100, marginal_cost=5)
n.add('Generator', 'grid_import', bus='power', p_nom=50, marginal_cost=60)
n.add('Generator', 'vent', bus='flue', p_nom=100, p_min_pu=-1, p_max_pu=0)
n.add('Generator', 'aux_supply', bus='aux', p_nom=10, marginal_cost=2)
n.add(
'Link',
'chp',
bus0='gasb',
bus1='power',
bus2='heat',
bus3='flue',
bus4='aux',
efficiency=0.4,
efficiency2=0.45,
efficiency3=0.1,
efficiency4=-0.02,
p_nom=60,
marginal_cost=1,
)
n.add('Load', 'homes', bus='power', p_set=20)
n.add('Load', 'district', bus='heat', p_set=18)
return n
The data¶
Every table this spec declares was first declared by a lower rung; its values here are in the prep above.