Skip to content

Rung 10: quadratic costs — a marginal cost quadratic in output

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 12587.437500000098 on both sides; structure ≠ CVaR 0 vs 1 — the file declares the tail's average on every run; PyPSA adds it only under a risk preference, and without one the objective prices it at zero and no row reads it; CVaR-a 0 vs 1 — the file declares each scenario's excess on every run; PyPSA adds it only under a risk preference, and without one no row reads it; CVaR-theta 0 vs 1 — the file declares the tail's start on every run; PyPSA adds it only under a risk preference, and without one no row reads it; size ✔ 60 rows · ≠ 24 vs 27 columns · ✔ 80 nonzeros; duals ✔ 60 rows; model for model: 8 blocks equal, 0 documented splits, 3 recorded deviations.

Rows and columns, PyPSA against specsolve, name for name
row PyPSA specsolve
Bus-nodal_balance 12 12
Generator-fix-p-lower 16 16
Generator-fix-p-upper 16 16
Link-fix-p-lower 8 8
Link-fix-p-upper 8 8
column PyPSA specsolve
CVaR 0 ≠ 1
CVaR-a 0 ≠ 1
CVaR-theta 0 ≠ 1
Generator-p 16 16
Link-p 8 8

The model

The same model, as math

A plain n.optimize(), and its multi-period and stochastic classes, in one file. Every second-stage quantity spans a scenario (a future dispatch is chosen in) and every asset stands in the investment periods its build year and lifetime span. A parameter spans scenario exactly when PyPSA reads it per scenario. Capacity is chosen once, before the future is known, and paid once per active period at its cost in expectation over the scenarios; operation is the expectation over the scenarios' weights, with a share priced at the tail through the CVaR rows, which stand only where that share is positive. A plain run feeds one scenario, one period, all-active masks and unit weights, and the model collapses to the standard one. A security-constrained run copies each branch flow limit once per outage in an outage set that a plain run leaves empty. Which snapshots an asset is active in, a scenario's weight, and the outage factors are data prep.

Sets

Symbol Meaning
\(\Xi\) index \(\xi\) — scenario — the futures dispatch is chosen in, each with a weight
\(\mathcal{T}\) index \(t\) — snapshot with \(\mathrm{snapshot\_period}: \mathcal{T} \to \mathcal{Y}\) — 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
\(\mathcal{Y}\) index \(y\) — period with \(\mathrm{snapshot\_period}: \mathcal{T} \to \mathcal{Y}\) — investment periods — PyPSA's investment_periods

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 \(\Xi \times \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 \(\Xi \times \mathcal{T} \times \mathcal{G}\) — least output, per unit of nominal power
\(\overline{\mathrm{p}}\) Generator_p_max_pu over \(\Xi \times \mathcal{T} \times \mathcal{G}\) — most output, per unit of nominal power — an availability profile
\(\mathrm{c}\) Generator_marginal_cost over \(\Xi \times \mathcal{T} \times \mathcal{G}\) — cost of one unit of output
\(\mathrm{c}^{(2)}\) Generator_marginal_cost_quadratic over \(\Xi \times \mathcal{T} \times \mathcal{G}\) — cost of the square of one unit of output
\(\mathrm{sgn}\) Generator_sign over \(\mathcal{G}\) — the sign output enters its bus's balance with — PyPSA's sign, 1 unless given, -1 for a unit that draws power. PyPSA refuses one that differs by scenario (consistency.py:1187)
\(\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 \(\Xi \times \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 \(\Xi \times \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 \(\Xi \times \mathcal{T} \times \mathcal{L}\) — most flow, per unit of nominal power
\(\eta\) Link_efficiency over \(\Xi \times \mathcal{T} \times \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. Read at the snapshot the flow arrives, so a delayed port delivers at its arrival snapshot's efficiency (constraints.py:1522)
\(\mathrm{d}^{f}\) Link_output_delay over \(\Xi \times \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. Each scenario takes its own. PyPSA 1.3.0 groups the ports by delay over all scenarios and shifts each group in every one, so a delay that differs by scenario delivers the flow twice (constraints.py:1269-1276, PyPSA/PyPSA#1941)
\(\mathrm{cyc}^{f}\) Link_output_cyclic_delay over \(\Xi \times \mathcal{O}\) — whether a delayed port's flow wraps from the end of its investment period — PyPSA's cyclic_delay, cyclic_delay2, …; where it does not, the flow still in transit at each period's first snapshots is lost. Each scenario takes its own, as the delay
\(\mathrm{c}^{f}\) Link_marginal_cost over \(\Xi \times \mathcal{T} \times \mathcal{L}\) — cost of one unit of flow
\(\mathrm{c}^{f,(2)}\) Link_marginal_cost_quadratic over \(\Xi \times \mathcal{T} \times \mathcal{L}\) — cost of the square of one unit of flow
\(\mathrm{com}^{f}\) Link_committable over \(\mathcal{L}\) — whether flow is gated by an on/off status decision
\(\mathrm{load}\) Load_p_set over \(\Xi \times \mathcal{T} \times \mathcal{D}\) — demand
\(\mathrm{sgn}^{\mathrm{load}}\) Load_sign over \(\mathcal{D}\) — the sign a load's demand enters its bus's balance with — PyPSA's sign, -1 unless given, 1 for a load that feeds its bus. PyPSA refuses one that differs by scenario (consistency.py:1187)
\(\mathrm{on}^{\mathrm{load}}\) Load_active over \(\mathcal{D}\) — whether a load stands in the model — PyPSA's active. A load has no build year and no lifetime, so the flag holds in every snapshot. PyPSA refuses one that differs by scenario (consistency.py:1195)
\(\pi\) scenario_weight over \(\Xi\) — PyPSA's scenario_weightings.weight — the probability of a future
\(\omega\) CVaR_omega (scalar) — PyPSA's risk_preference['omega'] — the share of operating cost priced at the tail rather than in expectation; zero recovers the risk-neutral model
\(\mathrm{w}^{y}\) period_weight_objective over \(\mathcal{Y}\) — PyPSA's investment_period_weightings.objective — what a period's cost weighs
\(\mathrm{on}\) Generator_active over \(\mathcal{T} \times \mathcal{G}\) — whether a generator stands in a snapshot's period — PyPSA's active, from build year and lifetime, data prep
\(\mathrm{on}^{f}\) Link_active over \(\mathcal{T} \times \mathcal{L}\) — whether a link stands in a snapshot's period — PyPSA's active, data prep

Variables

Symbol Meaning
\(p\) Generator_p over \(\Xi \times \mathcal{T} \times \mathcal{G}\) — Generator-p — output of a generator in a snapshot
\(f\) Link_p over \(\Xi \times \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
\(a\) CVaR_a over \(\Xi\) — CVaR-a — how far a scenario's operating cost exceeds the tail's start; nothing where it does not
\(\theta\) CVaR_theta (scalar) — CVaR-theta — where the tail starts, the value at risk
\(CVaR\) CVaR (scalar) — CVaR — the tail's average cost, what the objective prices at omega

Definitions

Symbol Meaning
\(\mathit{total\_cost}\) total_cost (scalar) — what the system costs — capacity once per active period at its expected cost over the scenarios, operation in expectation over the scenarios, and a share of it at the tail
\(\mathit{Bus\_injection}\) Bus_injection over \(\Xi \times \mathcal{T} \times \mathcal{N}\) — what every component puts into a bus, less what it takes out of it; PyPSA writes each term into the balance, and a load on its right-hand side
\(\mathit{risk\_weighted\_opex}\) risk_weighted_opex (scalar)
\(\mathit{Generator\_injection}\) Generator_injection over \(\Xi \times \mathcal{T} \times \mathcal{N}\)
\(\mathit{Link\_injection}\) Link_injection over \(\Xi \times \mathcal{T} \times \mathcal{N}\)
\(\mathrm{Load\_injection}\) Load_injection over \(\Xi \times \mathcal{T} \times \mathcal{N}\)
\(\mathit{Link\_output\_arrival}\) Link_output_arrival over \(\Xi \times \mathcal{T} \times \mathcal{O}\) — what a link delivers to an output port at a snapshot — its flow delayed by the port's delay within its investment period, times the port's efficiency at the snapshot the flow arrives; where the port is cyclic_delay the delayed flow wraps from the period's end, and where it is not the flow still in transit at the period's first snapshots is lost. A port that does not delay (delay zero) delivers its flow unshifted, cyclic or not
\(\mathit{scenario\_opex}\) scenario_opex over \(\Xi\) — what a future costs to run — every operating term, weighted by the snapshot's hours and its period, before the scenario's own weight; a start and a stop cost what they cost, unweighted, as PyPSA adds them (optimize.py:414-429)
\(\mathrm{Load\_demand}\) Load_demand over \(\Xi \times \mathcal{T} \times \mathcal{D}\) — what a load draws from its bus's balance — its demand times its sign where it is active, nothing where it is not, since PyPSA drops an inactive load from the balance (constraints.py:1537-1538)
\(\mathit{Generator\_opex}\) Generator_opex over \(\Xi\)
\(\mathit{Link\_opex}\) Link_opex over \(\Xi\)

\(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.

\(t \ominus^{\mathrm{relation}(t)} k\) denotes a translation counted inside the group a relation puts \(t\) in (shift(by=relation)), so a term never crosses out of its own group. The two modifiers take different slots — the group above, the fill below — so \(t \boxminus_{v}^{\mathrm{relation}(t)} k\) is both at once.

Objective

\[ \min \mathit{total\_cost} \]

Subject to

Generator_fix_p_lower

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

Generator_fix_p_upper

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

Link_fix_p_lower

\[ f_{\xi,t,l} \ge \underline{\mathrm{f}}_{\xi,t,l} \cdot \mathrm{f}^{\mathrm{nom}}_{\xi,l} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ l \in \mathcal{L} \,:\, \neg \mathrm{ext}^{f}_{l} \wedge \neg \mathrm{com}^{f}_{l} \wedge \mathrm{on}^{f}_{t,l} \]

Link_fix_p_upper

\[ f_{\xi,t,l} \le \overline{\mathrm{f}}_{\xi,t,l} \cdot \mathrm{f}^{\mathrm{nom}}_{\xi,l} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ l \in \mathcal{L} \,:\, \neg \mathrm{ext}^{f}_{l} \wedge \neg \mathrm{com}^{f}_{l} \wedge \mathrm{on}^{f}_{t,l} \]

Bus_nodal_balance

\[ \mathit{Bus\_injection}_{\xi,t,n} = 0 \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ n \in \mathcal{N} \]

Definitions

total_cost

\[ \mathit{total\_cost} = \mathit{risk\_weighted\_opex} \]

Bus_injection

\[ \mathit{Bus\_injection}_{\xi,t,n} = \mathit{Generator\_injection}_{\xi,t,n} + \mathit{Link\_injection}_{\xi,t,n} + \mathrm{Load\_injection}_{\xi,t,n} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ n \in \mathcal{N} \]

risk_weighted_opex

\[ \mathit{risk\_weighted\_opex} = \left( 1 - \omega \right) \cdot \left( \sum_{\xi \in \Xi} \pi_{\xi} \cdot \mathit{scenario\_opex}_{\xi} \right) + \omega \cdot CVaR \]

Generator_injection

\[ \mathit{Generator\_injection}_{\xi,t,n} = \sum_{g \in \mathcal{G} \,:\, \mathrm{Generator\_bus}(g) = n} \mathrm{sgn}_{g} \cdot p_{\xi,t,g} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ n \in \mathcal{N} \]

Link_injection

\[ \mathit{Link\_injection}_{\xi,t,n} = -\left( \sum_{l \in \mathcal{L} \,:\, \mathrm{Link\_bus0}(l) = n} f_{\xi,t,l} \right) + \sum_{o \in \mathcal{O} \,:\, \mathrm{Link\_output\_bus}(o) = n} \mathit{Link\_output\_arrival}_{\xi,t,o} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ n \in \mathcal{N} \]

Load_injection

\[ \mathrm{Load\_injection}_{\xi,t,n} = \sum_{d \in \mathcal{D} \,:\, \mathrm{Load\_bus}(d) = n} \mathrm{Load\_demand}_{\xi,t,d} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ n \in \mathcal{N} \]

Link_output_arrival

\[ \mathit{Link\_output\_arrival}_{\xi,t,o} = \begin{cases} f_{\xi,t \ominus^{\mathrm{snapshot\_period}(t)} \mathrm{d}^{f},\mathrm{Link\_output\_link}(o)} \cdot \eta_{\xi,t,o} & \text{if } \mathrm{cyc}^{f}_{\xi,o} \\ f_{\xi,t \boxminus_{0}^{\mathrm{snapshot\_period}(t)} \mathrm{d}^{f},\mathrm{Link\_output\_link}(o)} \cdot \eta_{\xi,t,o} & \text{otherwise} \end{cases} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ o \in \mathcal{O} \]

scenario_opex

\[ \mathit{scenario\_opex}_{\xi} = \mathit{Generator\_opex}_{\xi} + \mathit{Link\_opex}_{\xi} \qquad \forall\, \xi \in \Xi \]

Load_demand

\[ \mathrm{Load\_demand}_{\xi,t,d} = \begin{cases} \mathrm{sgn}^{\mathrm{load}}_{d} \cdot \mathrm{load}_{\xi,t,d} & \text{if } \mathrm{on}^{\mathrm{load}}_{d} \\ 0 & \text{otherwise} \end{cases} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ d \in \mathcal{D} \]

Generator_opex

\[ \mathit{Generator\_opex}_{\xi} = \sum_{t \in \mathcal{T}} \sum_{g \in \mathcal{G}} p_{\xi,t,g} \cdot \mathrm{c}_{\xi,t,g} \cdot \mathrm{w}_{t} \cdot \mathrm{w}^{y}_{\mathrm{snapshot\_period}(t)} + \sum_{t \in \mathcal{T}} \sum_{g \in \mathcal{G}} p_{\xi,t,g} \cdot p_{\xi,t,g} \cdot \mathrm{c}^{(2)}_{\xi,t,g} \cdot \mathrm{w}_{t} \cdot \mathrm{w}^{y}_{\mathrm{snapshot\_period}(t)} \qquad \forall\, \xi \in \Xi \]

Link_opex

\[ \mathit{Link\_opex}_{\xi} = \sum_{t \in \mathcal{T}} \sum_{l \in \mathcal{L}} f_{\xi,t,l} \cdot \mathrm{c}^{f}_{\xi,t,l} \cdot \mathrm{w}_{t} \cdot \mathrm{w}^{y}_{\mathrm{snapshot\_period}(t)} + \sum_{t \in \mathcal{T}} \sum_{l \in \mathcal{L}} f_{\xi,t,l} \cdot f_{\xi,t,l} \cdot \mathrm{c}^{f,(2)}_{\xi,t,l} \cdot \mathrm{w}_{t} \cdot \mathrm{w}^{y}_{\mathrm{snapshot\_period}(t)} \qquad \forall\, \xi \in \Xi \]

Variable domains

Generator_p

\[ p_{\xi,t,g} \in \mathbb{R} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{on}_{t,g} \]

Link_p

\[ f_{\xi,t,l} \in \mathbb{R} \qquad \forall\, \xi \in \Xi,\ t \in \mathcal{T},\ l \in \mathcal{L} \,:\, \mathrm{on}^{f}_{t,l} \]

CVaR_a

\[ a_{\xi} \ge 0 \qquad \forall\, \xi \in \Xi \]

CVaR_theta

\[ \theta \in \mathbb{R} \]

CVaR

\[ CVaR \in \mathbb{R} \]

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

description: A plain `n.optimize()`, and its multi-period and stochastic classes, in one file. Every second-stage
  quantity spans a `scenario` (a future dispatch is chosen in) and every asset stands in the investment
  `period`s its build year and lifetime span. A parameter spans `scenario` exactly when PyPSA reads it
  per scenario. Capacity is chosen once, before the future is known, and paid once per active period at
  its cost in expectation over the scenarios; operation is the expectation over the scenarios' weights,
  with a share priced at the tail through the CVaR rows, which stand only where that share is positive.
  A plain run feeds one scenario, one period, all-active masks and unit weights, and the model collapses
  to the standard one. A security-constrained run copies each branch flow limit once per outage in an
  `outage` set that a plain run leaves empty. Which snapshots an asset is active in, a scenario's weight,
  and the outage factors are data prep.
dimensions:
  scenario: {description: 'the futures dispatch is chosen in, each with a weight'}
  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'}
  period: {description: investment periods — PyPSA's `investment_periods`, dtype: int}
relations:
  snapshot_period: {description: the investment period a snapshot falls in, key: snapshot, values: period}
  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: [scenario, 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: [scenario, snapshot, generator]
  Generator_p_max_pu:
    description: most output, per unit of nominal power — an availability profile
    dims: [scenario, snapshot, generator]
  Generator_marginal_cost:
    description: cost of one unit of output
    dims: [scenario, snapshot, generator]
  Generator_marginal_cost_quadratic:
    description: cost of the square of one unit of output
    dims: [scenario, snapshot, generator]
  Generator_sign:
    description: the sign output enters its bus's balance with — PyPSA's `sign`, `1` unless given, `-1`
      for a unit that draws power. PyPSA refuses one that differs by scenario (`consistency.py:1187`)
    dims: [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: [scenario, 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: [scenario, snapshot, link]
  Link_p_max_pu:
    description: most flow, per unit of nominal power
    dims: [scenario, 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. Read at the snapshot the flow
      arrives, so a delayed port delivers at its arrival snapshot's efficiency (`constraints.py:1522`)
    dims: [scenario, snapshot, 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. Each scenario takes its own. PyPSA `1.3.0` groups the ports by
      delay over all scenarios and shifts each group in every one, so a delay that differs by scenario
      delivers the flow twice (`constraints.py:1269-1276`, PyPSA/PyPSA#1941)
    dims: [scenario, link_output]
    dtype: int
  Link_output_cyclic_delay:
    description: whether a delayed port's flow wraps from the end of its investment period — PyPSA's `cyclic_delay`,
      `cyclic_delay2`, …; where it does not, the flow still in transit at each period's first snapshots
      is lost. Each scenario takes its own, as the delay
    dims: [scenario, link_output]
    dtype: bool
  Link_marginal_cost:
    description: cost of one unit of flow
    dims: [scenario, snapshot, link]
  Link_marginal_cost_quadratic:
    description: cost of the square of one unit of flow
    dims: [scenario, snapshot, link]
  Link_committable:
    description: whether flow is gated by an on/off status decision
    dims: [link]
    dtype: bool
  Load_p_set:
    description: demand
    dims: [scenario, snapshot, load]
  Load_sign:
    description: the sign a load's demand enters its bus's balance with — PyPSA's `sign`, `-1` unless
      given, `1` for a load that feeds its bus. PyPSA refuses one that differs by scenario (`consistency.py:1187`)
    dims: [load]
  Load_active:
    description: whether a load stands in the model — PyPSA's `active`. A load has no build year and no
      lifetime, so the flag holds in every snapshot. PyPSA refuses one that differs by scenario (`consistency.py:1195`)
    dims: [load]
    dtype: bool
  scenario_weight:
    description: PyPSA's `scenario_weightings.weight` — the probability of a future
    dims: [scenario]
  CVaR_omega:
    description: PyPSA's `risk_preference['omega']` — the share of operating cost priced at the tail rather
      than in expectation; zero recovers the risk-neutral model
    dims: []
  period_weight_objective:
    description: PyPSA's `investment_period_weightings.objective` — what a period's cost weighs
    dims: [period]
  Generator_active:
    description: whether a generator stands in a snapshot's period — PyPSA's `active`, from build year
      and lifetime, data prep
    dims: [snapshot, generator]
    dtype: bool
  Link_active:
    description: whether a link stands in a snapshot's period — PyPSA's `active`, data prep
    dims: [snapshot, link]
    dtype: bool
variables:
  Generator_p:
    description: '`Generator-p` — output of a generator in a snapshot'
    dims: [scenario, snapshot, generator]
    where: Generator_active
  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: [scenario, snapshot, link]
    where: Link_active
  CVaR_a:
    description: '`CVaR-a` — how far a scenario''s operating cost exceeds the tail''s start; nothing where
      it does not'
    dims: [scenario]
    bounds: {lower: 0}
  CVaR_theta:
    description: '`CVaR-theta` — where the tail starts, the value at risk'
    dims: []
  CVaR:
    description: '`CVaR` — the tail''s average cost, what the objective prices at `omega`'
    dims: []
constraints:
  Generator_fix_p_lower:
    description: '`Generator-fix-p-lower` — a fixed generator outputs at least its minimum'
    dims: [scenario, snapshot, generator]
    where: not Generator_p_nom_extendable AND not Generator_committable AND Generator_active
    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: [scenario, snapshot, generator]
    where: not Generator_p_nom_extendable AND not Generator_committable AND Generator_active
    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: [scenario, snapshot, link]
    where: not Link_p_nom_extendable AND not Link_committable AND Link_active
    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: [scenario, snapshot, link]
    where: not Link_p_nom_extendable AND not Link_committable AND Link_active
    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, each process port drawing or delivering at its own rate and each passive branch
      carrying its flow, meets the load there, less half of every incident line''s and transformer''s
      loss — PyPSA dissipates a branch''s loss half at either end. Each generator, storage unit, store
      and load term enters with its component''s `sign` (`constraints.py:1428-1429`, `:1538`), and an
      inactive load not at all. A bus nothing is attached to has no row; PyPSA refuses one that carries
      load, and this file does not yet.'
    dims: [scenario, snapshot, bus]
    expression: Bus_injection == 0
expressions:
  total_cost:
    dims: []
    expression: risk_weighted_opex
    description: what the system costs — capacity once per active period at its expected cost over the
      scenarios, operation in expectation over the scenarios, and a share of it at the tail
  Bus_injection:
    dims: [scenario, snapshot, bus]
    expression: (Generator_injection + Link_injection) + Load_injection
    description: what every component puts into a bus, less what it takes out of it; PyPSA writes each
      term into the balance, and a load on its right-hand side
  risk_weighted_opex: {expression: '(1 - CVaR_omega) * sum(scenario_weight * scenario_opex, over=scenario)
      + CVaR_omega * CVaR'}
  Generator_injection: {expression: 'sum(Generator_sign * Generator_p, by=Generator_bus, over=generator,
      into=bus)'}
  Link_injection: {expression: '-sum(Link_p, by=Link_bus0, over=link, into=bus) + sum(Link_output_arrival,
      by=Link_output_bus, over=link_output, into=bus)'}
  Load_injection: {expression: 'sum(Load_demand, by=Load_bus, over=load, into=bus)'}
  Link_output_arrival:
    description: what a link delivers to an output port at a snapshot — its flow delayed by the port's
      `delay` within its investment period, times the port's efficiency at the snapshot the flow arrives;
      where the port is `cyclic_delay` the delayed flow wraps from the period's end, and where it is not
      the flow still in transit at the period's first snapshots is lost. A port that does not delay (`delay`
      zero) delivers its flow unshifted, cyclic or not
    dims: [scenario, snapshot, link_output]
    cases:
      wrapping: {when: Link_output_cyclic_delay, expression: 'shift(at(Link_p, by=Link_output_link, over=link,
          into=link_output), along=snapshot, offset=Link_output_delay, edge=''wrap'', by=snapshot_period,
          within=period) * Link_efficiency'}
    otherwise: shift(at(Link_p, by=Link_output_link, over=link, into=link_output), along=snapshot, offset=Link_output_delay,
      edge=0, by=snapshot_period, within=period) * Link_efficiency
  scenario_opex:
    dims: [scenario]
    expression: Generator_opex + Link_opex
    description: what a future costs to run — every operating term, weighted by the snapshot's hours and
      its period, before the scenario's own weight; a start and a stop cost what they cost, unweighted,
      as PyPSA adds them (`optimize.py:414-429`)
  Load_demand:
    description: what a load draws from its bus's balance — its demand times its sign where it is active,
      nothing where it is not, since PyPSA drops an inactive load from the balance (`constraints.py:1537-1538`)
    dims: [scenario, snapshot, load]
    cases:
      active: {when: Load_active, expression: Load_sign * Load_p_set}
    otherwise: 0
  Generator_opex: {expression: 'sum(sum(((Generator_p * Generator_marginal_cost) * snapshot_weightings_objective)
      * at(period_weight_objective, by=snapshot_period, over=period, into=snapshot), over=generator),
      over=snapshot) + sum(sum((((Generator_p * Generator_p) * Generator_marginal_cost_quadratic) * snapshot_weightings_objective)
      * at(period_weight_objective, by=snapshot_period, over=period, into=snapshot), over=generator),
      over=snapshot)'}
  Link_opex: {expression: 'sum(sum(((Link_p * Link_marginal_cost) * snapshot_weightings_objective) * at(period_weight_objective,
      by=snapshot_period, over=period, into=snapshot), over=link), over=snapshot) + sum(sum((((Link_p
      * Link_p) * Link_marginal_cost_quadratic) * snapshot_weightings_objective) * at(period_weight_objective,
      by=snapshot_period, over=period, into=snapshot), over=link), over=snapshot)'}
objective: {sense: minimize, expression: total_cost}

The prep — every table the spec declares, from the network — and the solve:

from differential.pypsa.prep import relation, static, varying, weighting


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),
        **{
            dim: pl.Series(dim, list(names(n.static(component).index).astype(str)), dtype=pl.String)
            for component, dim in DIM.items()
        },
        **scenarios(n),
        **periods(n),
        **carriers(n, multi),
    'Generator_bus': relation(n, 'Generator', 'bus'),
    'Link_bus0': relation(n, 'Link', 'bus0'),
    'Load_bus': relation(n, 'Load', 'bus'),
    'snapshot_weightings_objective': weighting(n, 'objective'),
    'Generator_sign': per_component('Generator', first_scenario(n.generators['sign'])),
    'Load_p_set': varying(n, 'Load', 'p_set'),
    'Load_sign': per_component('Load', first_scenario(loads['sign'])),
    'Load_active': per_component('Load', first_scenario(loads['active']), bool),
}

with sps.solve('differential/pypsa/rungs/rung_10_quadratic_costs.yaml', sources) as solution:
    solution.objective  # 12587.437500000098

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

"""Rung 10: quadratic costs — a marginal cost quadratic in output."""

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', 'village')
    n.add('Generator', 'steam', bus='north', p_nom=80, marginal_cost=5, marginal_cost_quadratic=0.08)
    n.add('Generator', 'engine', bus='north', p_nom=80, marginal_cost=20, marginal_cost_quadratic=0.01)
    n.add(
        'Link',
        'wire2',
        bus0='north',
        bus1='village',
        p_nom=40,
        p_min_pu=-1,
        efficiency=0.9,
        marginal_cost=1,
        marginal_cost_quadratic=0.02,
    )
    n.add('Load', 'village_load', bus='village', p_set=15)
    n.add('Load', 'extra10', bus='north', p_set=[30, 50, 40, 60])
    return n
n = build()
n.optimize(solver_name='highs')
n.objective  # 12587.437500000098

The data

Every table this spec declares was first declared by a lower rung; its values here are in the prep above.