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.
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_opt
/home/runner/work/PyPSA/PyPSA/pypsa/network/io.py:2085: FutureWarning: pandas infers the `str` dtype for string data since its version 3.0. PyPSA still converts it back to numpy object dtype on import, but will keep it from PyPSA 2.0 on. Set `pypsa.options.api.legacy_string_dtype` explicitly to suppress this warning. new_static = _coerce_string_dtypes(new_static)
INFO:pypsa.network.io:Imported network 'Model-Energy' has buses, carriers, generators, links, loads, storage_units, stores
/tmp/ipykernel_3958/1646313121.py:18: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. n.optimize(solver_name="gurobi")
INFO:linopy.model: Solve problem using Gurobi solver
INFO:linopy.model:Solver options: - log_to_console: False
INFO:linopy.io:Writing objective.
Writing constraints.: 0%| | 0/27 [00:00<?, ?it/s]
Writing constraints.: 81%|████████▏ | 22/27 [00:00<00:00, 217.75it/s]
Writing constraints.: 100%|██████████| 27/27 [00:00<00:00, 198.05it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 395.37it/s]
INFO:linopy.io: Writing time: 0.19s
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-u3bzeksh.lp
Reading time = 0.10 seconds
obj: 81765 rows, 35047 columns, 162099 nonzeros
Set parameter LogToConsole to value 0
INFO:linopy.constants: Optimization successful: Status: ok Termination condition: optimal Solution: 35047 primals, 105127 duals Objective: 6.55e+09 Solver: gurobi Runtime: 1.35s Dual bound: 6.55e+09 Solver model: available Solver message: 2
INFO: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.
name load shedding 10901.160000 wind 3864.989283 solar 5212.328279 nuclear 7639.029235 Name: p_nom_opt, dtype: float64
As 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).
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_opt
/tmp/ipykernel_3958/3454292476.py:3: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. n.optimize(solver_name="gurobi")
INFO:linopy.model: Solve problem using Gurobi solver
INFO:linopy.model:Solver options: - log_to_console: False
INFO:linopy.io:Writing objective.
Writing constraints.: 0%| | 0/29 [00:00<?, ?it/s]
Writing constraints.: 83%|████████▎ | 24/29 [00:00<00:00, 235.05it/s]
Writing constraints.: 100%|██████████| 29/29 [00:00<00:00, 216.10it/s]
Writing continuous variables.: 0%| | 0/13 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 13/13 [00:00<00:00, 404.24it/s]
Writing integer variables.: 0%| | 0/1 [00:00<?, ?it/s]
Writing integer variables.: 100%|██████████| 1/1 [00:00<00:00, 415.69it/s]
INFO:linopy.io: Writing time: 0.18s
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-1b9rh_5n.lp
Reading time = 0.09 seconds
obj: 81767 rows, 35048 columns, 162102 nonzeros
Set parameter LogToConsole to value 0
WARNING:linopy.solvers:Dual values of MILP couldn't be parsed
INFO:linopy.constants: Optimization successful: Status: ok Termination condition: optimal Solution: 35048 primals, 0 duals Objective: 6.55e+09 Solver: gurobi Runtime: 15.22s MIP gap: 1.46e-16 Dual bound: 6.55e+09 Solver model: available Solver message: 2
INFO:pypsa.optimization.optimize:No shadow prices were assigned to the network.
name load shedding 10901.160000 wind 2907.095790 solar 4437.946962 nuclear 8000.000000 Name: p_nom_opt, dtype: float64
From 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.
n.generators_t.p.loc["2019-01"].plot(figsize=(6, 3))
<Axes: xlabel='snapshot'>