Skip to content

Rung 8: modular and big-M — capacity in whole modules, built or already standing, and a committable unit whose capacity is also built

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 15915.0 on both sides; structure ✔ 28 constraints · 7 variables, name for name; size ✔ 191 rows · ✔ 80 columns · ✔ 379 nonzeros; duals — integer model, no duals; model for model: 36 blocks equal, 0 documented splits.

Rows and columns, PyPSA against specsolve, name for name
row PyPSA specsolve
Bus-nodal_balance 12 12
Generator-com-ext-p-lower 8 8
Generator-com-ext-p-lower-nonneg 4 4
Generator-com-ext-p-upper-bigM 8 8
Generator-com-ext-p-upper-cap 8 8
Generator-com-mod-p-lower 8 8
Generator-com-mod-p-upper 8 8
Generator-com-p-lower 4 4
Generator-com-p-upper 4 4
Generator-com-transition-shut-down 16 16
Generator-com-transition-start-up 16 16
Generator-ext-p_nom-lower 3 3
Generator-ext-p_nom-upper 3 3
Generator-fix-p-lower 8 8
Generator-fix-p-upper 8 8
Generator-p-ramp_limit_down-run-bigM 4 4
Generator-p-ramp_limit_down-shut-bigM 4 4
Generator-p-ramp_limit_up-run-bigM 4 4
Generator-p-ramp_limit_up-start-bigM 4 4
Generator-p_nom_modularity 1 1
Generator-shut_down-p-fixed-upper 12 12
Generator-shut_down-p_nom-variable-upper 4 4
Generator-start_up-p-fixed-upper 12 12
Generator-start_up-p_nom-variable-upper 4 4
Generator-status-p-fixed-upper 12 12
Generator-status-p_nom-variable-upper 4 4
Link-fix-p-lower 4 4
Link-fix-p-upper 4 4
column PyPSA specsolve
Generator-n_mod 1 1
Generator-p 24 24
Generator-p_nom 3 3
Generator-shut_down 16 16
Generator-start_up 16 16
Generator-status 16 16
Link-p 4 4

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{ru}\) Generator_ramp_limit_up over \(\mathcal{G}\) — most a generator may raise its output between snapshots, per unit of nominal power; no value means no limit
\(\mathrm{rd}\) Generator_ramp_limit_down over \(\mathcal{G}\) — most a generator may lower its output between snapshots, per unit of nominal power; no value means no limit
\(\mathrm{ru}^{\mathrm{up}}\) Generator_ramp_limit_start_up over \(\mathcal{G}\) — most output in the snapshot a unit starts, per unit of nominal power
\(\mathrm{rd}^{\mathrm{dn}}\) Generator_ramp_limit_shut_down over \(\mathcal{G}\) — most output in the snapshot before a unit stops, per unit of nominal power
\(\mathrm{u}^{0}\) Generator_status_initial over \(\mathcal{G}\) — one where the unit was on before the first snapshot, zero where off — PyPSA's up_time_before > 0, data prep
\(\mathrm{c}^{\mathrm{up}}\) Generator_start_up_cost over \(\mathcal{G}\) — cost of one start
\(\mathrm{c}^{\mathrm{dn}}\) Generator_shut_down_cost over \(\mathcal{G}\) — cost of one stop
\(\mathrm{c}^{\mathrm{on}}\) Generator_stand_by_cost over \(\mathcal{T} \times \mathcal{G}\) — cost of one snapshot spent on
\(\mathrm{p}^{\mathrm{mod}}\) Generator_p_nom_mod over \(\mathcal{G}\) — the module size a build comes in whole numbers of; no value means the build is continuous
\(\mathrm{N}^{\mathrm{fix}}\) Generator_modules_installed over \(\mathcal{G}\) — how many whole modules a committable build has in place: Generator_p_nom / Generator_p_nom_mod where a fixed build is modular, one where it is not, data prep. PyPSA refuses a fixed modular build whose nominal power is not a whole number of modules
\(\mathrm{M}\) Generator_big_m over \(\mathcal{G}\) — a bound safely above any feasible output — the build cap at full availability, data prep
\(\mathrm{nonneg}\) Generator_p_min_pu_nonneg over \(\mathcal{G}\) — true where none of the generator's own minimums-per-unit is negative — PyPSA's per-unit (p_min_pu >= 0).all(), data prep
\(\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
\(\underline{\mathrm{p}}^{\mathrm{nom}}\) Generator_p_nom_min over \(\mathcal{G}\) — least nominal power an extendable generator may be built at
\(\overline{\mathrm{p}}^{\mathrm{nom}}\) Generator_p_nom_max over \(\mathcal{G}\) — most nominal power an extendable generator may be built at
\(\mathrm{c}^{\mathrm{cap}}\) Generator_capital_cost over \(\mathcal{G}\) — cost of one unit of nominal power — PyPSA's capital_cost, periodized as an annuity in data prep

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
\(N\) Generator_n_mod over \(\mathcal{G}\) — Generator-n_mod — how many modules of an extendable modular build
\(u\) Generator_status over \(\mathcal{T} \times \mathcal{G}\) — Generator-status — how much of a committable unit is on: an integer the rows below cap at one, or at the module count where the build is modular
\(\mathit{up}\) Generator_start_up over \(\mathcal{T} \times \mathcal{G}\) — Generator-start_up — how much of a committable unit turns on this snapshot, capped as the status is
\(\mathit{dn}\) Generator_shut_down over \(\mathcal{T} \times \mathcal{G}\) — Generator-shut_down — how much of a committable unit turns off this snapshot, capped as the status is
\(P\) Generator_p_nom_ext over \(\mathcal{G}\) — Generator-p_nom — nominal power where it is a decision; the parameter of the same PyPSA name carries the fixed regime

Definitions

Symbol Meaning
\(\mathit{Generator\_previous\_status}\) Generator_previous_status over \(\mathcal{T} \times \mathcal{G}\) — the commitment state a generator carries into a snapshot — the state it brought into the horizon at the first, the previous snapshot's after that
\(\mathit{Generator\_previous\_p}\) Generator_previous_p over \(\mathcal{T} \times \mathcal{G}\) — the output a generator carries into a snapshot — nothing at the start of the horizon, which is why a unit that came in running carries no ramp row there
\(\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.

\(\mathrm{pos}(t)\) denotes where index \(t\) sits along its dimension's own order — the order shift steps along, not the order labels sort in — counted from \(0\). The index itself stays the coordinate, so \(t\) compares against labels and \(\mathrm{pos}(t)\) against positions.

Objective

\[ \min \sum_{t \in \mathcal{T},\ g \in \mathcal{G}} p_{t,g} \cdot \mathrm{c}_{t,g} \cdot \mathrm{w}_{t} + \sum_{t \in \mathcal{T},\ l \in \mathcal{L}} f_{t,l} \cdot \mathrm{c}^{f}_{t,l} \cdot \mathrm{w}_{t} + \sum_{g \in \mathcal{G}} P_{g} \cdot \mathrm{c}^{\mathrm{cap}}_{g} + \sum_{t \in \mathcal{T},\ g \in \mathcal{G}} u_{t,g} \cdot \mathrm{c}^{\mathrm{on}}_{t,g} \cdot \mathrm{w}_{t} + \sum_{t \in \mathcal{T},\ g \in \mathcal{G}} \mathit{up}_{t,g} \cdot \mathrm{c}^{\mathrm{up}}_{g} + \sum_{t \in \mathcal{T},\ g \in \mathcal{G}} \mathit{dn}_{t,g} \cdot \mathrm{c}^{\mathrm{dn}}_{g} \]

Subject to

Generator_fix_p_lower

\[ p_{t,g} \ge \underline{\mathrm{p}}_{t,g} \cdot \mathrm{p}^{\mathrm{nom}}_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \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\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \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\, t \in \mathcal{T},\ l \in \mathcal{L} \,:\, \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\, t \in \mathcal{T},\ l \in \mathcal{L} \,:\, \neg \mathrm{ext}^{f}_{l} \]

Generator_ext_p_nom_lower

\[ P_{g} \ge \underline{\mathrm{p}}^{\mathrm{nom}}_{g} \qquad \forall\, g \in \mathcal{G} \,:\, \mathrm{ext}_{g} \]

Generator_ext_p_nom_upper

\[ P_{g} \le \overline{\mathrm{p}}^{\mathrm{nom}}_{g} \qquad \forall\, g \in \mathcal{G} \,:\, \mathrm{ext}_{g} \wedge \overline{\mathrm{p}}^{\mathrm{nom}}_{g} \text{ is defined} \]

Generator_com_p_lower

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

Generator_com_p_upper

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

Generator_com_transition_start_up

\[ \mathit{up}_{t,g} \ge u_{t,g} - \mathit{Generator\_previous\_status}_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \]

Generator_com_transition_shut_down

\[ \mathit{dn}_{t,g} \ge \mathit{Generator\_previous\_status}_{t,g} - u_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \]

Generator_p_ramp_limit_up_run_big_m

\[ p_{t,g} - \mathit{Generator\_previous\_p}_{t,g} \le \mathrm{ru}_{g} \cdot P_{g} + \mathrm{M}_{g} - \mathrm{M}_{g} \cdot \mathit{Generator\_previous\_status}_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{ru}_{g} \text{ is defined} \wedge \left( \mathrm{pos}(t) > 0 \vee \mathrm{u}^{0}_{g} = 0 \right) \]

Generator_p_ramp_limit_up_start_big_m

\[ p_{t,g} - \mathit{Generator\_previous\_p}_{t,g} \le \mathrm{ru}^{\mathrm{up}}_{g} \cdot P_{g} + \mathrm{M}_{g} - \mathrm{M}_{g} \cdot \mathit{up}_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{ru}_{g} \text{ is defined} \wedge \left( \mathrm{pos}(t) > 0 \vee \mathrm{u}^{0}_{g} = 0 \right) \]

Generator_p_ramp_limit_down_run_big_m

\[ \mathit{Generator\_previous\_p}_{t,g} - p_{t,g} \le \mathrm{rd}_{g} \cdot P_{g} + \mathrm{M}_{g} - \mathrm{M}_{g} \cdot u_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{rd}_{g} \text{ is defined} \wedge \left( \mathrm{pos}(t) > 0 \vee \mathrm{u}^{0}_{g} = 0 \right) \]

Generator_p_ramp_limit_down_shut_big_m

\[ \mathit{Generator\_previous\_p}_{t,g} - p_{t,g} \le \mathrm{rd}^{\mathrm{dn}}_{g} \cdot P_{g} + \mathrm{M}_{g} - \mathrm{M}_{g} \cdot \mathit{dn}_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{rd}_{g} \text{ is defined} \wedge \left( \mathrm{pos}(t) > 0 \vee \mathrm{u}^{0}_{g} = 0 \right) \]

Generator_p_nom_modularity

\[ P_{g} = \mathrm{p}^{\mathrm{mod}}_{g} \cdot N_{g} \qquad \forall\, g \in \mathcal{G} \,:\, \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \]

Generator_com_ext_p_upper_cap

\[ p_{t,g} \le \overline{\mathrm{p}}_{t,g} \cdot P_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \neg \left( \mathrm{p}^{\mathrm{mod}}_{g} > 0 \right) \]

Generator_com_ext_p_upper_big_m

\[ p_{t,g} \le \mathrm{M}_{g} \cdot u_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \neg \left( \mathrm{p}^{\mathrm{mod}}_{g} > 0 \right) \]

Generator_com_ext_p_lower

\[ p_{t,g} \ge \underline{\mathrm{p}}_{t,g} \cdot P_{g} + \mathrm{M}_{g} \cdot u_{t,g} - \mathrm{M}_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \neg \left( \mathrm{p}^{\mathrm{mod}}_{g} > 0 \right) \]

Generator_com_ext_p_lower_nonneg

\[ p_{t,g} \ge 0 \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{nonneg}_{g} \wedge \neg \left( \mathrm{p}^{\mathrm{mod}}_{g} > 0 \right) \]

Generator_com_mod_p_lower

\[ p_{t,g} \ge \underline{\mathrm{p}}_{t,g} \cdot \mathrm{p}^{\mathrm{mod}}_{g} \cdot u_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \]

Generator_com_mod_p_upper

\[ p_{t,g} \le \overline{\mathrm{p}}_{t,g} \cdot \mathrm{p}^{\mathrm{mod}}_{g} \cdot u_{t,g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \]

Generator_status_p_fixed_upper

\[ u_{t,g} \le \mathrm{N}^{\mathrm{fix}}_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \neg \left( \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \right) \]

Generator_start_up_p_fixed_upper

\[ \mathit{up}_{t,g} \le \mathrm{N}^{\mathrm{fix}}_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \neg \left( \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \right) \]

Generator_shut_down_p_fixed_upper

\[ \mathit{dn}_{t,g} \le \mathrm{N}^{\mathrm{fix}}_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \neg \left( \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \right) \]

Generator_status_p_nom_variable_upper

\[ u_{t,g} \le N_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \]

Generator_start_up_p_nom_variable_upper

\[ \mathit{up}_{t,g} \le N_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \]

Generator_shut_down_p_nom_variable_upper

\[ \mathit{dn}_{t,g} \le N_{g} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \wedge \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \]

Bus_nodal_balance

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

Definitions

Generator_previous_status

\[ \mathit{Generator\_previous\_status}_{t,g} = \begin{cases} \mathrm{u}^{0}_{g} & \text{if } \mathrm{pos}(t) = 0 \\ u_{t - 1,g} & \text{otherwise} \end{cases} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \]

Generator_previous_p

\[ \mathit{Generator\_previous\_p}_{t,g} = \begin{cases} 0 & \text{if } \mathrm{pos}(t) = 0 \\ p_{t - 1,g} & \text{otherwise} \end{cases} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \]

Link_output_arrival

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

Variable domains

Generator_p

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

Link_p

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

Generator_n_mod

\[ N_{g} \ge 0, N_{g} \in \mathbb{Z} \qquad \forall\, g \in \mathcal{G} \,:\, \mathrm{ext}_{g} \wedge \mathrm{p}^{\mathrm{mod}}_{g} > 0 \]

Generator_status

\[ u_{t,g} \ge 0, u_{t,g} \in \mathbb{Z} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \]

Generator_start_up

\[ \mathit{up}_{t,g} \ge 0, \mathit{up}_{t,g} \in \mathbb{Z} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \]

Generator_shut_down

\[ \mathit{dn}_{t,g} \ge 0, \mathit{dn}_{t,g} \in \mathbb{Z} \qquad \forall\, t \in \mathcal{T},\ g \in \mathcal{G} \,:\, \mathrm{com}_{g} \]

Generator_p_nom_ext

\[ P_{g} \in \mathbb{R} \qquad \forall\, g \in \mathcal{G} \,:\, \mathrm{ext}_{g} \]

The spec, differential/pypsa/rungs/rung_08_modular_big_m.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
  Generator_ramp_limit_up:
    description: most a generator may raise its output between snapshots, per unit of nominal power; no
      value means no limit
    dims: [generator]
  Generator_ramp_limit_down:
    description: most a generator may lower its output between snapshots, per unit of nominal power; no
      value means no limit
    dims: [generator]
  Generator_ramp_limit_start_up:
    description: most output in the snapshot a unit starts, per unit of nominal power
    dims: [generator]
  Generator_ramp_limit_shut_down:
    description: most output in the snapshot before a unit stops, per unit of nominal power
    dims: [generator]
  Generator_status_initial:
    description: one where the unit was on before the first snapshot, zero where off — PyPSA's `up_time_before
      > 0`, data prep
    dims: [generator]
    dtype: int
  Generator_start_up_cost:
    description: cost of one start
    dims: [generator]
  Generator_shut_down_cost:
    description: cost of one stop
    dims: [generator]
  Generator_stand_by_cost:
    description: cost of one snapshot spent on
    dims: [snapshot, generator]
  Generator_p_nom_mod:
    description: the module size a build comes in whole numbers of; no value means the build is continuous
    dims: [generator]
  Generator_modules_installed:
    description: 'how many whole modules a committable build has in place: `Generator_p_nom / Generator_p_nom_mod`
      where a fixed build is modular, one where it is not, data prep. PyPSA refuses a fixed modular build
      whose nominal power is not a whole number of modules'
    dims: [generator]
  Generator_big_m:
    description: a bound safely above any feasible output — the build cap at full availability, data prep
    dims: [generator]
  Generator_p_min_pu_nonneg:
    description: true where none of the generator's own minimums-per-unit is negative — PyPSA's per-unit
      `(p_min_pu >= 0).all()`, data prep
    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]
  Generator_p_nom_min:
    description: least nominal power an extendable generator may be built at
    dims: [generator]
  Generator_p_nom_max:
    description: most nominal power an extendable generator may be built at
    dims: [generator]
  Generator_capital_cost:
    description: cost of one unit of nominal power — PyPSA's `capital_cost`, periodized as an annuity
      in data prep
    dims: [generator]
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]
  Generator_n_mod:
    description: '`Generator-n_mod` — how many modules of an extendable modular build'
    dims: [generator]
    where: Generator_p_nom_extendable AND Generator_p_nom_mod > 0
    domain: integer
    bounds: {lower: 0}
  Generator_status:
    description: '`Generator-status` — how much of a committable unit is on: an integer the rows below
      cap at one, or at the module count where the build is modular'
    dims: [snapshot, generator]
    where: Generator_committable
    domain: integer
    bounds: {lower: 0}
  Generator_start_up:
    description: '`Generator-start_up` — how much of a committable unit turns on this snapshot, capped
      as the status is'
    dims: [snapshot, generator]
    where: Generator_committable
    domain: integer
    bounds: {lower: 0}
  Generator_shut_down:
    description: '`Generator-shut_down` — how much of a committable unit turns off this snapshot, capped
      as the status is'
    dims: [snapshot, generator]
    where: Generator_committable
    domain: integer
    bounds: {lower: 0}
  Generator_p_nom_ext:
    description: '`Generator-p_nom` — nominal power where it is a decision; the parameter of the same
      PyPSA name carries the fixed regime'
    dims: [generator]
    where: Generator_p_nom_extendable
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
  Generator_ext_p_nom_lower:
    description: '`Generator-ext-p_nom-lower` — the chosen build is at least its floor'
    dims: [generator]
    where: Generator_p_nom_extendable
    expression: Generator_p_nom_ext >= Generator_p_nom_min
  Generator_ext_p_nom_upper:
    description: '`Generator-ext-p_nom-upper` — the chosen build is at most its cap; a cap of infinity
      is no row'
    dims: [generator]
    where: Generator_p_nom_extendable AND Generator_p_nom_max
    expression: Generator_p_nom_ext <= Generator_p_nom_max
  Generator_com_p_lower:
    description: '`Generator-com-p-lower` — a committed unit outputs at least its minimum; off, at least
      nothing'
    dims: [snapshot, generator]
    where: Generator_committable AND not Generator_p_nom_extendable
    expression: Generator_p >= Generator_p_min_pu * Generator_p_nom * Generator_status
  Generator_com_p_upper:
    description: '`Generator-com-p-upper` — a committed unit outputs at most what is available; off, at
      most nothing'
    dims: [snapshot, generator]
    where: Generator_committable AND not Generator_p_nom_extendable
    expression: Generator_p <= Generator_p_max_pu * Generator_p_nom * Generator_status
  Generator_com_transition_start_up:
    description: '`Generator-com-transition-start-up` — turning on is a start, counted against the state
      the unit carried into the snapshot'
    dims: [snapshot, generator]
    where: Generator_committable
    expression: Generator_start_up >= Generator_status - Generator_previous_status
  Generator_com_transition_shut_down:
    description: '`Generator-com-transition-shut-down` — turning off is a stop, counted against the state
      the unit carried into the snapshot'
    dims: [snapshot, generator]
    where: Generator_committable
    expression: Generator_shut_down >= Generator_previous_status - Generator_status
  Generator_p_ramp_limit_up_run_big_m:
    description: '`Generator-p-ramp_limit_up-run-bigM` — a committed extendable unit raises output no
      faster than its limit of the chosen build; the big M releases the row in the snapshot it turns on'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_ramp_limit_up AND (position(snapshot)
      > 0 OR Generator_status_initial == 0)
    expression: Generator_p - Generator_previous_p <= Generator_ramp_limit_up * Generator_p_nom_ext +
      Generator_big_m - Generator_big_m * Generator_previous_status
  Generator_p_ramp_limit_up_start_big_m:
    description: '`Generator-p-ramp_limit_up-start-bigM` — in the snapshot it turns on, a committed extendable
      unit ramps no further than its start-up ramp of the chosen build; the big M releases the row everywhere
      else'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_ramp_limit_up AND (position(snapshot)
      > 0 OR Generator_status_initial == 0)
    expression: Generator_p - Generator_previous_p <= Generator_ramp_limit_start_up * Generator_p_nom_ext
      + Generator_big_m - Generator_big_m * Generator_start_up
  Generator_p_ramp_limit_down_run_big_m:
    description: '`Generator-p-ramp_limit_down-run-bigM` — a committed extendable unit lowers output no
      faster than its limit of the chosen build; the big M releases the row in the snapshot it turns off'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_ramp_limit_down AND (position(snapshot)
      > 0 OR Generator_status_initial == 0)
    expression: Generator_previous_p - Generator_p <= Generator_ramp_limit_down * Generator_p_nom_ext
      + Generator_big_m - Generator_big_m * Generator_status
  Generator_p_ramp_limit_down_shut_big_m:
    description: '`Generator-p-ramp_limit_down-shut-bigM` — in the snapshot it turns off, a committed
      extendable unit ramps no further than its shut-down ramp of the chosen build; the big M releases
      the row everywhere else'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_ramp_limit_down AND (position(snapshot)
      > 0 OR Generator_status_initial == 0)
    expression: Generator_previous_p - Generator_p <= Generator_ramp_limit_shut_down * Generator_p_nom_ext
      + Generator_big_m - Generator_big_m * Generator_shut_down
  Generator_p_nom_modularity:
    description: '`Generator-p_nom_modularity` — the chosen build is a whole number of modules'
    dims: [generator]
    where: Generator_p_nom_extendable AND Generator_p_nom_mod > 0
    expression: Generator_p_nom_ext == Generator_p_nom_mod * Generator_n_mod
  Generator_com_ext_p_upper_cap:
    description: '`Generator-com-ext-p-upper-cap` — a committed extendable unit outputs at most what is
      available of the chosen build, whatever its status'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND NOT (Generator_p_nom_mod > 0)
    expression: Generator_p <= Generator_p_max_pu * Generator_p_nom_ext
  Generator_com_ext_p_upper_big_m:
    description: '`Generator-com-ext-p-upper-bigM` — off, a unit outputs nothing; on, the big M is no
      bound'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND NOT (Generator_p_nom_mod > 0)
    expression: Generator_p <= Generator_big_m * Generator_status
  Generator_com_ext_p_lower:
    description: '`Generator-com-ext-p-lower` — a committed extendable unit outputs at least its minimum
      of the chosen build; off, the big M releases the row'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND NOT (Generator_p_nom_mod > 0)
    expression: Generator_p >= Generator_p_min_pu * Generator_p_nom_ext + Generator_big_m * Generator_status
      - Generator_big_m
  Generator_com_ext_p_lower_nonneg:
    description: '`Generator-com-ext-p-lower-nonneg` — where no minimum-per-unit is negative, output is
      also plainly non-negative, a row the big-M lower cannot assert while the unit is off'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_p_min_pu_nonneg AND NOT
      (Generator_p_nom_mod > 0)
    expression: Generator_p >= 0
  Generator_com_mod_p_lower:
    description: '`Generator-com-mod-p-lower` — a committed modular unit outputs at least its minimum
      of one module, whether the build is fixed or a decision'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_mod > 0
    expression: Generator_p >= Generator_p_min_pu * Generator_p_nom_mod * Generator_status
  Generator_com_mod_p_upper:
    description: '`Generator-com-mod-p-upper` — a committed modular unit outputs at most one module''s
      share, whether the build is fixed or a decision'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_mod > 0
    expression: Generator_p <= Generator_p_max_pu * Generator_p_nom_mod * Generator_status
  Generator_status_p_fixed_upper:
    description: '`Generator-status-p-fixed-upper` — a status is at most the modules in place, an explicit
      row as PyPSA writes it: one where the build is not modular, and the fixed build''s whole count of
      modules where it is'
    dims: [snapshot, generator]
    where: Generator_committable AND NOT (Generator_p_nom_extendable AND Generator_p_nom_mod > 0)
    expression: Generator_status <= Generator_modules_installed
  Generator_start_up_p_fixed_upper:
    description: '`Generator-start_up-p-fixed-upper` — a start is at most the modules in place, an explicit
      row as PyPSA writes it: one where the build is not modular, and the fixed build''s whole count of
      modules where it is'
    dims: [snapshot, generator]
    where: Generator_committable AND NOT (Generator_p_nom_extendable AND Generator_p_nom_mod > 0)
    expression: Generator_start_up <= Generator_modules_installed
  Generator_shut_down_p_fixed_upper:
    description: '`Generator-shut_down-p-fixed-upper` — a stop is at most the modules in place, an explicit
      row as PyPSA writes it: one where the build is not modular, and the fixed build''s whole count of
      modules where it is'
    dims: [snapshot, generator]
    where: Generator_committable AND NOT (Generator_p_nom_extendable AND Generator_p_nom_mod > 0)
    expression: Generator_shut_down <= Generator_modules_installed
  Generator_status_p_nom_variable_upper:
    description: '`Generator-status-p_nom-variable-upper` — a modular unit is on only where a module is
      built'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_p_nom_mod > 0
    expression: Generator_status <= Generator_n_mod
  Generator_start_up_p_nom_variable_upper:
    description: '`Generator-start_up-p_nom-variable-upper` — a modular unit starts only where a module
      is built'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_p_nom_mod > 0
    expression: Generator_start_up <= Generator_n_mod
  Generator_shut_down_p_nom_variable_upper:
    description: '`Generator-shut_down-p_nom-variable-upper` — a modular unit stops only where a module
      is built'
    dims: [snapshot, generator]
    where: Generator_committable AND Generator_p_nom_extendable AND Generator_p_nom_mod > 0
    expression: Generator_shut_down <= Generator_n_mod
  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:
  Generator_previous_status:
    description: the commitment state a generator carries into a snapshot — the state it brought into
      the horizon at the first, the previous snapshot's after that
    dims: [snapshot, generator]
    cases:
      opening: {when: position(snapshot) == 0, expression: Generator_status_initial}
    otherwise: shift(Generator_status, along=snapshot, offset=1)
  Generator_previous_p:
    description: the output a generator carries into a snapshot — nothing at the start of the horizon,
      which is why a unit that came in running carries no ramp row there
    dims: [snapshot, generator]
    cases:
      opening: {when: position(snapshot) == 0, expression: 0}
    otherwise: shift(Generator_p, along=snapshot, offset=1)
  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) + sum(Generator_p_nom_ext * Generator_capital_cost)
    + sum(Generator_status * Generator_stand_by_cost * snapshot_weightings_objective) + sum(Generator_start_up
    * Generator_start_up_cost) + sum(Generator_shut_down * Generator_shut_down_cost)}

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 _modules_installed(n: pypsa.Network) -> pd.DataFrame:
    """The whole modules a build has standing — ``p_nom / p_nom_mod`` where a fixed build is modular, one where it is not.

    PyPSA refuses a fixed modular build whose nominal power is not a whole number of modules, so the
    division is exact and left unrounded: a fraction here is a network this prep should not have taken.
    """
    generators = n.static('Generator')
    modular = ~generators['p_nom_extendable'] & (generators.get('p_nom_mod', 0.0) > 0)
    counts = generators['p_nom'].where(modular, 1.0) / generators.get('p_nom_mod', 1.0).where(modular, 1.0)
    return pd.DataFrame(keyed(generators.index, 'generator') | {'value': counts.to_numpy()})


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'),
    'Generator_ramp_limit_up': static(n, 'Generator', 'ramp_limit_up').dropna(),
    'Generator_ramp_limit_down': static(n, 'Generator', 'ramp_limit_down').dropna(),
    'Generator_ramp_limit_start_up': static(n, 'Generator', 'ramp_limit_start_up').fillna({'value': 1.0}),
    'Generator_ramp_limit_shut_down': static(n, 'Generator', 'ramp_limit_shut_down').fillna({'value': 1.0}),
    'Generator_status_initial': pd.DataFrame(
            keyed(generators.index, 'generator')
            | {
                'value': (generators['up_time_before'] > 0).astype(int).to_numpy(),
            }
        ),
    'Generator_start_up_cost': static(n, 'Generator', 'start_up_cost'),
    'Generator_shut_down_cost': static(n, 'Generator', 'shut_down_cost'),
    'Generator_stand_by_cost': varying(n, 'Generator', 'stand_by_cost'),
    'Generator_p_nom_mod': static(n, 'Generator', 'p_nom_mod').query('value > 0'),
    'Generator_modules_installed': _modules_installed(n),
    'Generator_big_m': pd.DataFrame(keyed(big_m.index, 'generator') | {'value': big_m.to_numpy()}),
    'Generator_p_min_pu_nonneg': pd.DataFrame(
            keyed(generators.index, 'generator')
            | {
                'value': (get_switchable_as_dense(n, 'Generator', 'p_min_pu') >= 0).all().to_numpy(),
            }
        ),
    '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'),
    'Generator_p_nom_min': static(n, 'Generator', 'p_nom_min'),
    'Generator_p_nom_max': static(n, 'Generator', 'p_nom_max'),
    'Generator_capital_cost': static(n, 'Generator', 'capital_cost'),
}

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

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

"""Rung 8: modular and big-M — capacity in whole modules, built or already standing, and a committable unit whose capacity is also built."""

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', 'mill')
    n.add(
        'Generator',
        'block',
        bus='mill',
        p_nom_extendable=True,
        committable=True,
        p_nom_mod=25,
        p_nom_max=100,
        capital_cost=30,
        marginal_cost=20,
        p_min_pu=0.2,
        up_time_before=0,
    )
    n.add(
        'Generator',
        'flex',
        bus='mill',
        p_nom_extendable=True,
        committable=True,
        p_nom_max=80,
        capital_cost=50,
        marginal_cost=10,
        p_min_pu=0.3,
        up_time_before=0,
        ramp_limit_up=0.25,
        ramp_limit_down=0.25,
    )
    n.add(
        'Generator',
        'sink',
        bus='mill',
        p_nom_extendable=True,
        committable=True,
        p_nom_max=30,
        capital_cost=40,
        marginal_cost=15,
        p_min_pu=-0.2,
        up_time_before=0,
    )
    n.add(
        'Generator',
        'array',
        bus='mill',
        committable=True,
        p_nom=90,
        p_nom_mod=30,
        marginal_cost=12,
        p_min_pu=0.2,
        up_time_before=0,
    )
    n.add('Load', 'mill_load', bus='mill', p_set=[40, 80, 120, 60])
    return n
n = build()
n.optimize(solver_name='highs')
n.objective  # 15915.0

The data

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

Generator_big_m.csv

generator,value
block,100.0
flex,80.0
sink,30.0

Generator_p_min_pu_nonneg.csv

generator,value
array,true
block,true
coal,true
flex,true
gas,true
sink,false

Generator_p_nom_mod.csv

generator,value
array,30.0
block,25.0