Modular Expansion
In this example, we demonstrate how to handle the modular expansion feature in PyPSA. Modular expansion allows you to specify discrete steps for the expansion of components. This is particularly useful for technologies that can only be built in fixed block sizes, such as nuclear generators.
We start by loading the 3-hourly resolved model.energy example and adding a nuclear generator as expansion option (with ramp limits of 1%/h and a minimum part load of 70%). Initially, we allow the nuclear generator to be built in continuous sizes.
In [2]:
Copied!
import pypsa
from pypsa.costs import annuity
n = pypsa.examples.model_energy()
n.add(
"Generator",
"nuclear",
bus="electricity",
p_nom_extendable=True,
marginal_cost=15,
capital_cost=annuity(0.07, 50) * 8_000_000,
p_min_pu=0.7,
ramp_limit_up=0.03,
ramp_limit_down=0.03,
)
n.optimize(solver_name="gurobi")
n.generators.p_nom_optimport pypsa from pypsa.costs import annuity n = pypsa.examples.model_energy() n.add( "Generator", "nuclear", bus="electricity", p_nom_extendable=True, marginal_cost=15, capital_cost=annuity(0.07, 50) * 8_000_000, p_min_pu=0.7, ramp_limit_up=0.03, ramp_limit_down=0.03, ) n.optimize(solver_name="gurobi") n.generators.p_nom_opt
/tmp/ipykernel_4261/1646313121.py:4: PyPSA2Warning: pandas reads strings as `str` dtype since 3.0. PyPSA converts them back to object dtype on import, but keeps `str` from PyPSA 2.0 on. Set `pypsa.options.future.string_dtype` to True to opt in now or to False to keep the current behaviour.
n = pypsa.examples.model_energy()INFO:pypsa.network.io:Imported network 'Model-Energy' has buses, carriers, generators, links, loads, storage_units, stores/tmp/ipykernel_4261/1646313121.py:18: PyPSA2Warning: From PyPSA 2.0 on, the objective leaves out the capital costs of existing capacity on extendable assets, which improves LP conditioning. Pass `include_objective_constant` to `n.optimize` to choose per call. Set `pypsa.options.future.exclude_objective_constant` to True to opt in now or to False to keep the current behaviour.
n.optimize(solver_name="gurobi")INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io:Writing objective.Writing constraints.: 0%| | 0/27 [00:00<?, ?it/s]Writing constraints.: 89%|████████▉ | 24/27 [00:00<00:00, 232.30it/s]Writing constraints.: 100%|██████████| 27/27 [00:00<00:00, 216.45it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 411.68it/s]INFO:linopy.io: Writing time: 0.18sSet parameter WLSAccessIDSet parameter WLSSecretSet parameter LicenseID to value 2537914Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deRead LP format model from file /tmp/linopy-problem-9kkx2h2t.lpReading time = 0.10 secondsobj: 81765 rows, 35047 columns, 162099 nonzerosSet parameter LogToConsole to value 0INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 35047 primals, 105127 duals
Objective: 6.55e+09
Solver: gurobi
Runtime: 1.33s
Dual bound: 6.55e+09
Solver model: available
Solver message: 2INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-fix-p-lower, Generator-fix-p-upper, Generator-ext-p-lower, Generator-ext-p-upper, Generator-p-ramp_limit_up, Generator-p-ramp_limit_down, Link-ext-p-lower, Link-ext-p-upper, Store-ext-e-lower, Store-ext-e-upper, Store-ext-p-lower, Store-ext-p-upper, Store-ext-p_store-lower, Store-ext-p_store-upper, StorageUnit-ext-p_dispatch-lower, StorageUnit-ext-p_dispatch-upper, StorageUnit-ext-p_store-lower, StorageUnit-ext-p_store-upper, StorageUnit-ext-state_of_charge-lower, StorageUnit-ext-state_of_charge-upper, StorageUnit-energy_balance, Store-energy_balance were not assigned to the network.Out[2]:
name
load shedding 10901.160000
wind 3864.989283
solar 5212.328279
nuclear 7639.029235
Name: p_nom_opt, dtype: float64As optimal capacity for the nuclear generator, we obtain 7,639 MW.
Now, let's assume that the nuclear generator can only be built in discrete steps of 1,000 MW. We can set the p_nom_mod attribute to 1,000 MW, which introduces an integer variable that constraints the optimised capacity to be a multiple of 1,000 MW. To constrain the option space for the integer variable, and help the solver a bit, we can also set the upper limit p_nom_max to 10,000 MW.
This problem is solved as a mixed-integer linear program (MILP), which will take longer to solve than the previous continuous linear program (LP).
In [3]:
Copied!
n.generators.loc["nuclear", "p_nom_mod"] = 1000
n.generators.loc["nuclear", "p_nom_max"] = 10000
n.optimize(solver_name="gurobi")
n.generators.p_nom_optn.generators.loc["nuclear", "p_nom_mod"] = 1000 n.generators.loc["nuclear", "p_nom_max"] = 10000 n.optimize(solver_name="gurobi") n.generators.p_nom_opt
INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io:Writing objective.Writing constraints.: 0%| | 0/29 [00:00<?, ?it/s]Writing constraints.: 86%|████████▌ | 25/29 [00:00<00:00, 242.81it/s]Writing constraints.: 100%|██████████| 29/29 [00:00<00:00, 225.32it/s]Writing continuous variables.: 0%| | 0/13 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 13/13 [00:00<00:00, 414.61it/s]Writing integer variables.: 0%| | 0/1 [00:00<?, ?it/s]Writing integer variables.: 100%|██████████| 1/1 [00:00<00:00, 429.57it/s]INFO:linopy.io: Writing time: 0.18sSet parameter WLSAccessIDSet parameter WLSSecretSet parameter LicenseID to value 2537914Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deRead LP format model from file /tmp/linopy-problem-htq8qetk.lpReading time = 0.10 secondsobj: 81767 rows, 35048 columns, 162102 nonzerosSet parameter LogToConsole to value 0WARNING:linopy.solvers:Dual values of MILP couldn't be parsedINFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 35048 primals, 0 duals
Objective: 6.55e+09
Solver: gurobi
Runtime: 15.20s
MIP gap: 1.46e-16
Dual bound: 6.55e+09
Solver model: available
Solver message: 2INFO:pypsa.optimization.optimize:No shadow prices were assigned to the network.Out[3]:
name
load shedding 10901.160000
wind 2907.095790
solar 4437.946962
nuclear 8000.000000
Name: p_nom_opt, dtype: float64From the re-optimised results, we can see that the optimal capacity for the nuclear generator is now 8,000 MW, a multiple of 1,000 MW.
As a short concluding excursion into the results, we can also illustrate how the optimal dispatch of this nuclear generator is constrained by ramp limits and minimum part loads in January 2019.
In [4]:
Copied!
n.generators_t.p.loc["2019-01"].plot(figsize=(6, 3))n.generators_t.p.loc["2019-01"].plot(figsize=(6, 3))
Out[4]:
<Axes: xlabel='snapshot'>
2026-11-05
2025-08-04