Skip to content

Rung 1: transport — two buses, two generators, one controllable link

One rung of the PyPSA corpus: the file pypsa.yaml projected onto what this network builds, bound to that network, and held to what PyPSA solves it to.

✔ Verified against pypsa 1.3.0 — objective 7182.222222222221 on both sides; structure ✔ 7 constraints · 2 variables, name for name; size ✔ 45 rows · ✔ 16 columns · ✔ 57 nonzeros; duals ✔ 45 rows; model for model: 10 blocks equal, 0 documented splits.

Rows and columns, PyPSA against lpspec, name for name
row PyPSA lpspec
Bus-nodal_balance 8 8
Generator-fix-p-lower 12 12
Generator-fix-p-upper 12 12
Generator-p_set 4 4
Link-fix-p-lower 4 4
Link-fix-p-upper 4 4
Link-p_set 1 1
column PyPSA lpspec
Generator-p 12 12
Link-p 4 4

The model

The same model, as math

The model a plain n.optimize() builds, stated 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 — 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}\) — 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},\enspace \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{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
\(\mathrm{p}^{\mathrm{set}}\) Generator_p_set over \(\mathcal{T} \times \mathcal{G}\) — a given output schedule; a generator without one has no row here
\(\mathrm{f}^{\mathrm{set}}\) Link_p_set over \(\mathcal{T} \times \mathcal{L}\) — a given flow schedule; a link without one has no row here

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

Objective

\[\min \sum_{t \in \mathcal{T},\enspace g \in \mathcal{G}} p_{t,g} \cdot \mathrm{c}_{t,g} \cdot \mathrm{w}_{t} + \sum_{t \in \mathcal{T},\enspace l \in \mathcal{L}} f_{t,l} \cdot \mathrm{c}^{f}_{t,l} \cdot \mathrm{w}_{t}\]

Subject to

Generator_fix_p_lower

\[p_{t,g} \ge \underline{\mathrm{p}}_{t,g} \cdot \mathrm{p}^{\mathrm{nom}}_{g} \qquad \forall\thinspace t \in \mathcal{T},\enspace g \in \mathcal{G} \thinspace:\thinspace \neg \mathrm{ext}_{g} \wedge \neg \mathrm{com}_{g}\]

Generator_fix_p_upper

\[p_{t,g} \le \overline{\mathrm{p}}_{t,g} \cdot \mathrm{p}^{\mathrm{nom}}_{g} \qquad \forall\thinspace t \in \mathcal{T},\enspace g \in \mathcal{G} \thinspace:\thinspace \neg \mathrm{ext}_{g} \wedge \neg \mathrm{com}_{g}\]

Link_fix_p_lower

\[f_{t,l} \ge \underline{\mathrm{f}}_{t,l} \cdot \mathrm{f}^{\mathrm{nom}}_{l} \qquad \forall\thinspace t \in \mathcal{T},\enspace l \in \mathcal{L} \thinspace:\thinspace \neg \mathrm{ext}^{f}_{l}\]

Link_fix_p_upper

\[f_{t,l} \le \overline{\mathrm{f}}_{t,l} \cdot \mathrm{f}^{\mathrm{nom}}_{l} \qquad \forall\thinspace t \in \mathcal{T},\enspace l \in \mathcal{L} \thinspace:\thinspace \neg \mathrm{ext}^{f}_{l}\]

Generator_p_set

\[p_{t,g} = \mathrm{p}^{\mathrm{set}}_{t,g} \qquad \forall\thinspace t \in \mathcal{T},\enspace g \in \mathcal{G} \thinspace:\thinspace \mathrm{p}^{\mathrm{set}}_{t,g} \text{ is defined}\]

Link_p_set

\[f_{t,l} = \mathrm{f}^{\mathrm{set}}_{t,l} \qquad \forall\thinspace t \in \mathcal{T},\enspace l \in \mathcal{L} \thinspace:\thinspace \mathrm{f}^{\mathrm{set}}_{t,l} \text{ is defined}\]

Bus_nodal_balance

\[\sum_{g \in \mathcal{G} \thinspace:\thinspace \mathrm{Generator\_bus}(g) = n} p_{t,g} - \left( \sum_{l \in \mathcal{L} \thinspace:\thinspace \mathrm{Link\_bus0}(l) = n} f_{t,l} \right) + \sum_{o \in \mathcal{O} \thinspace:\thinspace \mathrm{Link\_output\_bus}(o) = n} f_{t,\mathrm{Link\_output\_link}(o)} \cdot \eta_{o} = \sum_{d \in \mathcal{D} \thinspace:\thinspace \mathrm{Load\_bus}(d) = n} \mathrm{load}_{t,d} \qquad \forall\thinspace t \in \mathcal{T},\enspace n \in \mathcal{N}\]

Variable domains

Generator_p

\[p_{t,g} \in \mathbb{R} \qquad \forall\thinspace t \in \mathcal{T},\enspace g \in \mathcal{G}\]

Link_p

\[f_{t,l} \in \mathbb{R} \qquad \forall\thinspace t \in \mathcal{T},\enspace l \in \mathcal{L}\]

The model, differential/pypsa/rungs/rung_01_transport.yaml — the file projected onto what this rung builds:

description: The model a plain `n.optimize()` builds, stated 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'}
lookups:
  Generator_bus: {description: the bus a generator sits on, over: generator, into: bus}
  Link_bus0: {description: the bus a link leaves, over: link, into: bus}
  Link_output_link: {description: the link an output port belongs to, over: link_output, into: 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 lookup, so the file states
      any number of them', over: link_output, into: bus}
  Load_bus: {description: the bus a load sits on, over: load, into: 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_marginal_cost:
    description: cost of one unit of flow
    dims: [snapshot, link]
  Load_p_set:
    description: demand
    dims: [snapshot, load]
  Generator_p_set:
    description: a given output schedule; a generator without one has no row here
    dims: [snapshot, generator]
  Link_p_set:
    description: a given flow schedule; a link without one has no row here
    dims: [snapshot, link]
variables:
  Generator_p:
    description: '`Generator-p` — output of a generator in a snapshot'
    foreach: [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'
    foreach: [snapshot, link]
constraints:
  Generator_fix_p_lower:
    description: '`Generator-fix-p-lower` — a fixed generator outputs at least its minimum'
    foreach: [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'
    foreach: [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'
    foreach: [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'
    foreach: [snapshot, link]
    where: not Link_p_nom_extendable
    expression: Link_p <= Link_p_max_pu * Link_p_nom
  Generator_p_set:
    description: '`Generator-p_set` — output pinned to the given schedule, wherever one is given'
    foreach: [snapshot, generator]
    where: Generator_p_set
    expression: Generator_p == Generator_p_set
  Link_p_set:
    description: '`Link-p_set` — flow pinned to the given schedule, wherever one is given'
    foreach: [snapshot, link]
    where: Link_p_set
    expression: Link_p == Link_p_set
  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 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.'
    foreach: [snapshot, bus]
    expression: sum(Generator_p, by=Generator_bus) - sum(Link_p, by=Link_bus0) + sum(at(Link_p, by=Link_output_link)
      * Link_efficiency, by=Link_output_bus) == sum(Load_p_set, by=Load_bus)
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 binding — every table the model declares, from the network — and the solve:

from differential.pypsa.prep import lookup, 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)
        efficiencies = links.get(f'efficiency{suffix}', pd.Series(1.0, index=links.index)).astype(float)
        frame = pd.DataFrame(
            keyed(links.index, 'link') | {'bus': buses.to_numpy(), 'value': efficiencies.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) -> pd.DataFrame:
    """One column of the long port table keyed by ``link_output`` — what each port names, or the efficiency it carries."""
    ports = _link_ports(n)
    keys = [key for key in ('scenario', 'link_output') if key in ports.columns]
    return ports[[*keys, 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': lookup(n, 'Generator', 'bus'),
    'Link_bus0': lookup(n, 'Link', 'bus0'),
    'Link_output_link': _per_port(n, 'link'),
    'Link_output_bus': _per_port(n, 'bus'),
    'Load_bus': lookup(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_marginal_cost': varying(n, 'Link', 'marginal_cost'),
    'Load_p_set': varying(n, 'Load', 'p_set'),
    'Generator_p_set': varying(n, 'Generator', 'p_set').dropna(),
    'Link_p_set': varying(n, 'Link', 'p_set').dropna(),
}

with lps.solve('differential/pypsa/rungs/rung_01_transport.yaml', sources) as solution:
    solution.objective  # 7182.222222222221

The network, rung_01_transport.py in the corpus — the spine plus what this rung adds:

"""Rung 1: transport — two buses, two generators, one controllable link."""

from __future__ import annotations

from math import nan

import spine


def build():
    """The spine plus this rung's additions, as a ``pypsa.Network``."""
    n = spine.build()
    n.links_t.p_set['wire'] = [10, nan, nan, nan]
    n.add('Generator', 'must_run', bus='south', p_nom=10, marginal_cost=0, p_set=[5, 5, 5, 5])
    return n
n = build()
n.optimize(solver_name='highs')
n.objective  # 7182.222222222221

The data

The tables this rung is the first to declare (27), as the binding produced them:

Generator_bus.csv

generator,Generator_bus
coal,north
gas,south
must_run,south

Generator_committable.csv

generator,value
coal,false
gas,false
must_run,false

Generator_marginal_cost.csv

snapshot,generator,value
2015-01-01T00:00:00.000000,coal,10.0
2015-01-01T00:00:00.000000,gas,30.0
2015-01-01T00:00:00.000000,must_run,0.0
2015-01-01T01:00:00.000000,coal,10.0
2015-01-01T01:00:00.000000,gas,30.0
2015-01-01T01:00:00.000000,must_run,0.0
2015-01-01T02:00:00.000000,coal,10.0
2015-01-01T02:00:00.000000,gas,30.0
2015-01-01T02:00:00.000000,must_run,0.0
2015-01-01T03:00:00.000000,coal,10.0
2015-01-01T03:00:00.000000,gas,30.0
2015-01-01T03:00:00.000000,must_run,0.0

Generator_p_max_pu.csv

snapshot,generator,value
2015-01-01T00:00:00.000000,coal,1.0
2015-01-01T00:00:00.000000,gas,1.0
2015-01-01T00:00:00.000000,must_run,1.0
2015-01-01T01:00:00.000000,coal,1.0
2015-01-01T01:00:00.000000,gas,1.0
2015-01-01T01:00:00.000000,must_run,1.0
2015-01-01T02:00:00.000000,coal,1.0
2015-01-01T02:00:00.000000,gas,1.0
2015-01-01T02:00:00.000000,must_run,1.0
2015-01-01T03:00:00.000000,coal,1.0
2015-01-01T03:00:00.000000,gas,1.0
2015-01-01T03:00:00.000000,must_run,1.0

Generator_p_min_pu.csv

snapshot,generator,value
2015-01-01T00:00:00.000000,coal,0.0
2015-01-01T00:00:00.000000,gas,0.0
2015-01-01T00:00:00.000000,must_run,0.0
2015-01-01T01:00:00.000000,coal,0.0
2015-01-01T01:00:00.000000,gas,0.0
2015-01-01T01:00:00.000000,must_run,0.0
2015-01-01T02:00:00.000000,coal,0.0
2015-01-01T02:00:00.000000,gas,0.0
2015-01-01T02:00:00.000000,must_run,0.0
2015-01-01T03:00:00.000000,coal,0.0
2015-01-01T03:00:00.000000,gas,0.0
2015-01-01T03:00:00.000000,must_run,0.0

Generator_p_nom.csv

generator,value
coal,100.0
gas,100.0
must_run,10.0

Generator_p_nom_extendable.csv

generator,value
coal,false
gas,false
must_run,false

Generator_p_set.csv

snapshot,generator,value
2015-01-01T00:00:00.000000,must_run,5.0
2015-01-01T01:00:00.000000,must_run,5.0
2015-01-01T02:00:00.000000,must_run,5.0
2015-01-01T03:00:00.000000,must_run,5.0

Link_bus0.csv

link,Link_bus0
wire,north

Link_efficiency.csv

link_output,value
wire_bus1,0.9

Link_marginal_cost.csv

snapshot,link,value
2015-01-01T00:00:00.000000,wire,0.0
2015-01-01T01:00:00.000000,wire,0.0
2015-01-01T02:00:00.000000,wire,0.0
2015-01-01T03:00:00.000000,wire,0.0

Link_output_bus.csv

link_output,Link_output_bus
wire_bus1,south

Link_output_link.csv

link_output,Link_output_link
wire_bus1,wire

Link_p_max_pu.csv

snapshot,link,value
2015-01-01T00:00:00.000000,wire,1.0
2015-01-01T01:00:00.000000,wire,1.0
2015-01-01T02:00:00.000000,wire,1.0
2015-01-01T03:00:00.000000,wire,1.0

Link_p_min_pu.csv

snapshot,link,value
2015-01-01T00:00:00.000000,wire,-1.0
2015-01-01T01:00:00.000000,wire,-1.0
2015-01-01T02:00:00.000000,wire,-1.0
2015-01-01T03:00:00.000000,wire,-1.0

Link_p_nom.csv

link,value
wire,40.0

Link_p_nom_extendable.csv

link,value
wire,false

Link_p_set.csv

snapshot,link,value
2015-01-01T00:00:00.000000,wire,10.0

Load_bus.csv

load,Load_bus
north_load,north
south_load,south

Load_p_set.csv

snapshot,load,value
2015-01-01T00:00:00.000000,north_load,30.0
2015-01-01T00:00:00.000000,south_load,40.0
2015-01-01T01:00:00.000000,north_load,30.0
2015-01-01T01:00:00.000000,south_load,40.0
2015-01-01T02:00:00.000000,north_load,30.0
2015-01-01T02:00:00.000000,south_load,40.0
2015-01-01T03:00:00.000000,north_load,30.0
2015-01-01T03:00:00.000000,south_load,40.0

bus.csv

bus
north
south

generator.csv

generator
coal
gas
must_run

link.csv

link
wire

link_output.csv

link_output
wire_bus1

load.csv

load
north_load
south_load

snapshot.csv

snapshot
2015-01-01T00:00:00.000000
2015-01-01T01:00:00.000000
2015-01-01T02:00:00.000000
2015-01-01T03:00:00.000000

snapshot_weightings_objective.csv

snapshot,value
2015-01-01T00:00:00.000000,2.0
2015-01-01T01:00:00.000000,1.5
2015-01-01T02:00:00.000000,2.5
2015-01-01T03:00:00.000000,3.0