Note
You can download this example as a Jupyter notebook or start it in interactive mode.
Single Node Sector Coupling#
[1]:
import matplotlib.pyplot as plt
import pandas as pd
import pypsa
plt.style.use("bmh")
Previous Capacity Expansion Model#
To explore sector-coupling options with PyPSA, let’s load the capacity expansion model we built for the electricity system and add sector-coupling technologies and demands on top.
This example has single node for Germany and 4-hourly temporal resolution for a year. It has wind and solar solar generation, an OCGT generator as well as battery and hydrogen storage to supply a fixed electricity demand.
Some sector-coupling technologies have multiple ouputs (e.g. CHP plants producing heat and power). PyPSA can automatically handle links have more than one input (bus0
) and/or output (i.e. bus1
, bus2
, bus3
) with a given efficieny (efficiency
, efficiency2
, efficiency3
).
[2]:
n = pypsa.Network(
"https://tubcloud.tu-berlin.de/s/pzytNg9gtkgPpXc/download/network-cem.nc"
)
INFO:pypsa.io:Retrieving network data from https://tubcloud.tu-berlin.de/s/pzytNg9gtkgPpXc/download/network-cem.nc.
WARNING:pypsa.io:Importing network from PyPSA version v0.27.1 while current version is v0.34.0. Read the release notes at https://pypsa.readthedocs.io/en/latest/release_notes.html to prepare your network for import.
INFO:pypsa.io:Imported network network-cem.nc has buses, carriers, generators, global_constraints, loads, storage_units
[3]:
n
[3]:
Unnamed PyPSA Network
---------------------
Components:
- Bus: 1
- Carrier: 6
- Generator: 4
- GlobalConstraint: 1
- Load: 1
- StorageUnit: 2
Snapshots: 2190
Hydrogen Production#
The following example shows how to model the components of hydrogen storage separately, i.e. electrolysis, fuel cell and storage.
First, let’s remove the simplified hydrogen storage representation:
[4]:
n.remove("StorageUnit", "hydrogen storage underground")
Add a separate Bus
for the hydrogen energy carrier:
[5]:
n.add("Bus", "hydrogen")
[5]:
Index(['hydrogen'], dtype='object')
Add a Link
for the hydrogen electrolysis:
[6]:
n.add(
"Link",
"electrolysis",
bus0="electricity",
bus1="hydrogen",
carrier="electrolysis",
p_nom_extendable=True,
efficiency=0.7,
capital_cost=50e3, # €/MW/a
)
[6]:
Index(['electrolysis'], dtype='object')
Add a Link
for the fuel cell which reconverts hydrogen to electricity:
[7]:
n.add(
"Link",
"fuel cell",
bus0="hydrogen",
bus1="electricity",
carrier="fuel cell",
p_nom_extendable=True,
efficiency=0.5,
capital_cost=120e3, # €/MW/a
)
[7]:
Index(['fuel cell'], dtype='object')
Add a Store
for the hydrogen storage:
[8]:
n.add(
"Store",
"hydrogen storage",
bus="hydrogen",
carrier="hydrogen storage",
capital_cost=140, # €/MWh/a
e_nom_extendable=True,
e_cyclic=True, # cyclic state of charge
)
[8]:
Index(['hydrogen storage'], dtype='object')
We can also add a hydrogen demand to the hydrogen bus.
In the example below, we add a constant hydrogen demand the size of the electricity demand.
[9]:
p_set = n.loads_t.p_set["demand"].mean()
[10]:
p_set
[10]:
np.float64(54671.88812785388)
[11]:
n.add("Load", "hydrogen demand", bus="hydrogen", carrier="hydrogen", p_set=p_set) # MW
[11]:
Index(['hydrogen demand'], dtype='object')
Heat Demand#
For the heat demand, we create another bus and connect a load with the heat demand time series to it:
[12]:
n.add("Bus", "heat")
[12]:
Index(['heat'], dtype='object')
[13]:
url = "https://tubcloud.tu-berlin.de/s/mSkHERH8fJCKNXx/download/heat-load-example.csv"
p_set = pd.read_csv(url, index_col=0, parse_dates=True).squeeze()
[14]:
p_set.head()
[14]:
snapshot
2015-01-01 00:00:00 61726.043437
2015-01-01 04:00:00 108787.133591
2015-01-01 08:00:00 101508.988082
2015-01-01 12:00:00 90475.260586
2015-01-01 16:00:00 96307.755312
Name: 0, dtype: float64
[15]:
n.add("Load", "heat demand", carrier="heat", bus="heat", p_set=p_set)
[15]:
Index(['heat demand'], dtype='object')
[16]:
n.loads_t.p_set.div(1e3).plot(figsize=(12, 4), ylabel="GW")
[16]:
<Axes: xlabel='snapshot', ylabel='GW'>

Heat pumps#
To model heat pumps, first we have to calculate the coefficient of performance (COP) profile based on the temperature profile of the heat source.
In the example below, we calculate the COP for an air-sourced heat pump with a sink temperature of 55° C and a population-weighted ambient temperature profile for Germany.
The heat pump performance is given by the following function:
where \(\Delta T = T_{sink} - T_{source}\).
[17]:
def cop(t_source, t_sink=55):
delta_t = t_sink - t_source
return 6.81 - 0.121 * delta_t + 0.000630 * delta_t**2
[18]:
url = "https://tubcloud.tu-berlin.de/s/S4jRAQMP5Te96jW/download/ninja_weather_country_DE_merra-2_population_weighted.csv"
temp = pd.read_csv(url, skiprows=2, index_col=0, parse_dates=True).loc[
"2015", "temperature"
][::4]
[19]:
cop(temp).plot(figsize=(10, 2), ylabel="COP");

[20]:
plt.scatter(temp, cop(temp))
plt.xlabel("temperature [°C]")
plt.ylabel("COP [-]")
[20]:
Text(0, 0.5, 'COP [-]')

Once we have calculated the heat pump coefficient of performance, we can add the heat pump to the network as a Link
. We use the parameter efficiency
to incorporate the COP.
[21]:
n.add(
"Link",
"heat pump",
carrier="heat pump",
bus0="electricity",
bus1="heat",
efficiency=cop(temp),
p_nom_extendable=True,
capital_cost=3e5, # €/MWe/a
)
[21]:
Index(['heat pump'], dtype='object')
Let’s also add a resistive heater as backup technology:
[22]:
n.add(
"Link",
"resistive heater",
carrier="resistive heater",
bus0="electricity",
bus1="heat",
efficiency=0.9,
capital_cost=1e4, # €/MWe/a
p_nom_extendable=True,
)
[22]:
Index(['resistive heater'], dtype='object')
Combined Heat-and-Power (CHP)#
In the following, we are going to add gas-fired combined heat-and-power plants (CHPs). Today, these would use fossil gas, but in the example below we assume green methane with relatively high marginal costs. Since we have no other net emission technology, we can remove the CO\(_2\) limit.
[23]:
n.remove("GlobalConstraint", "CO2Limit")
Then, we explicitly represent the energy carrier gas:
[24]:
n.add("Bus", "gas", carrier="gas")
[24]:
Index(['gas'], dtype='object')
And add a Store
of gas, which can be depleted (up to 100 TWh) with fuel costs of 150 €/MWh.
[25]:
n.add(
"Store",
"gas storage",
carrier="gas storage",
e_initial=100e6, # MWh
e_nom=100e6, # MWh
bus="gas",
marginal_cost=150, # €/MWh_th
)
[25]:
Index(['gas storage'], dtype='object')
When we do this, we have to model the OCGT power plant as link which converts gas to electricity, not as generator.
[26]:
n.remove("Generator", "OCGT")
[27]:
n.add(
"Link",
"OCGT",
bus0="gas",
bus1="electricity",
carrier="OCGT",
p_nom_extendable=True,
capital_cost=20000, # €/MW/a
efficiency=0.4,
)
[27]:
Index(['OCGT'], dtype='object')
Next, we are going to add a combined heat-and-power (CHP) plant with fixed heat-power ratio (i.e. backpressure operation). If you want to model flexible heat-power ratios, have a look at this example: https://pypsa.readthedocs.io/en/latest/examples/power-to-gas-boiler-chp.html
[28]:
n.add(
"Link",
"CHP",
bus0="gas",
bus1="electricity",
bus2="heat",
carrier="CHP",
p_nom_extendable=True,
capital_cost=40000,
efficiency=0.4,
efficiency2=0.4,
)
[28]:
Index(['CHP'], dtype='object')
Electric Vehicles#
To model electric vehicles, we first create another bus for the electric vehicles.
[29]:
n.add("Bus", "EV", carrier="EV")
[29]:
Index(['EV'], dtype='object')
Then, we can attach the electricity consumption of electric vehicles to this bus:
[30]:
url = "https://tubcloud.tu-berlin.de/s/9r5bMSbzzQiqG7H/download/electric-vehicle-profile-example.csv"
p_set = pd.read_csv(url, index_col=0, parse_dates=True).squeeze()
[31]:
p_set.loc["2015-01-01"].div(1e3).plot(figsize=(4, 4), ylabel="GW")
[31]:
<Axes: xlabel='snapshot', ylabel='GW'>

[32]:
n.add("Load", "EV demand", bus="EV", carrier="EV demand", p_set=p_set)
[32]:
Index(['EV demand'], dtype='object')
Let’s have a quick look at how the heat, electricity, constant hydrogen and electric vehicle demands relate to each other:
[33]:
n.loads_t.p_set.div(1e3).plot(figsize=(10, 3), ylabel="GW")
plt.axhline(
n.loads.loc["hydrogen demand", "p_set"] / 1e3, label="hydrogen demand", color="m"
)
plt.legend()
[33]:
<matplotlib.legend.Legend at 0x767ded68b9d0>

The electric vehicles can only be charged when they are plugged-in. Below we load an availability profile telling us what share of electric vehicles is plugged-in at home – we only assume home charging in this example.
[34]:
url = "https://tubcloud.tu-berlin.de/s/E3PBWPfYaWwCq7a/download/electric-vehicle-availability-example.csv"
availability_profile = pd.read_csv(url, index_col=0, parse_dates=True).squeeze()
[35]:
availability_profile.loc["2015-01-01"].plot(ylim=(0, 1))
[35]:
<Axes: xlabel='snapshot'>

Then, we can add a link for the electric vehicle charger using assumption about the number of EVs and their charging rates.
[36]:
number_cars = 40e6 # number of EV cars
bev_charger_rate = 0.011 # 3-phase EV charger with 11 kW
p_nom = number_cars * bev_charger_rate
[37]:
n.add(
"Link",
"EV charger",
bus0="electricity",
bus1="EV",
p_nom=p_nom,
carrier="EV charger",
p_max_pu=availability_profile,
efficiency=0.9,
)
[37]:
Index(['EV charger'], dtype='object')
We can also allow vehicle-to-grid operation (i.e. electric vehicles inject power into the grid):
[38]:
n.add(
"Link",
"V2G",
bus0="EV",
bus1="electricity",
p_nom=p_nom,
carrier="V2G",
p_max_pu=availability_profile,
efficiency=0.9,
)
[38]:
Index(['V2G'], dtype='object')
The demand-side management potential we model as a store. This is not unlike a battery storage, but we impose additional constraints on when the store needs to be charged to a certain level (e.g. 75% full every morning).
[39]:
bev_energy = 0.05 # average battery size of EV in MWh
bev_dsm_participants = 0.5 # share of cars that do smart charging
e_nom = number_cars * bev_energy * bev_dsm_participants
[40]:
url = "https://tubcloud.tu-berlin.de/s/K62yACBRTrxLTia/download/dsm-profile-example.csv"
dsm_profile = (
pd.read_csv(url, index_col=0, parse_dates=True).squeeze().shift(2, fill_value=0)
)
[41]:
dsm_profile.loc["2015-01-01"].plot(figsize=(5, 2), ylim=(0, 1))
[41]:
<Axes: xlabel='snapshot'>

[42]:
n.add(
"Store",
"EV DSM",
bus="EV",
carrier="EV battery",
e_cyclic=True, # state of charge at beginning = state of charge at the end
e_nom=e_nom,
e_min_pu=dsm_profile.loc[n.snapshots],
)
[42]:
Index(['EV DSM'], dtype='object')
Then, we can solve the fully sector-coupled model altogether including electricity, passenger transport, hydrogen and heating.
[43]:
n.optimize(solver_name="highs")
WARNING:pypsa.consistency:The following stores have carriers which are not defined:
Index(['hydrogen storage', 'gas storage', 'EV DSM'], dtype='object', name='Store')
WARNING:pypsa.consistency:The following loads have carriers which are not defined:
Index(['hydrogen demand', 'heat demand', 'EV demand'], dtype='object', name='Load')
WARNING:pypsa.consistency:The following links have carriers which are not defined:
Index(['electrolysis', 'fuel cell', 'heat pump', 'resistive heater', 'CHP',
'EV charger', 'V2G'],
dtype='object', name='Link')
WARNING:pypsa.consistency:Encountered nan's in static data for columns ['efficiency2'] of component 'Link'.
WARNING:pypsa.consistency:The following buses have carriers which are not defined:
Index(['electricity', 'hydrogen', 'heat', 'gas', 'EV'], dtype='object', name='Bus')
INFO:linopy.model: Solve problem using Highs solver
INFO:linopy.io:Writing objective.
Writing constraints.: 100%|██████████| 27/27 [00:00<00:00, 63.95it/s]
Writing continuous variables.: 100%|██████████| 11/11 [00:00<00:00, 145.58it/s]
INFO:linopy.io: Writing time: 0.52s
Running HiGHS 1.10.0 (git hash: fd86653): Copyright (c) 2025 HiGHS under MIT licence terms
LP linopy-problem-yiutdf9u has 94181 rows; 43811 cols; 185110 nonzeros
Coefficient ranges:
Matrix [1e-04, 6e+00]
Cost [4e-02, 3e+05]
Bound [0e+00, 0e+00]
RHS [2e+03, 1e+08]
Presolving model
47129 rows, 42760 cols, 137007 nonzeros 0s
Dependent equations search running on 13140 equations with time limit of 1000.00s
Dependent equations search removed 0 rows and 0 nonzeros in 0.00s (limit = 1000.00s)
40560 rows, 36191 cols, 123869 nonzeros 0s
Presolve : Reductions: rows 40560(-53621); columns 36191(-7620); elements 123869(-61241)
Solving the presolved LP
Using EKK dual simplex solver - serial
Iteration Objective Infeasibilities num(sum)
0 0.0000000000e+00 Ph1: 0(0) 0s
40009 5.7916433334e+10 Pr: 12506(4.27419e+11); Du: 0(1.6744e-06) 5s
49950 8.9910382224e+10 Pr: 5059(2.96953e+10); Du: 0(1.88214e-06) 11s
58467 1.1508738683e+11 Pr: 7117(2.27287e+11); Du: 0(1.07404e-06) 16s
66736 1.2931005093e+11 Pr: 13075(8.20246e+12); Du: 0(1.08823e-06) 22s
71910 1.3550196758e+11 Pr: 11849(3.46178e+11); Du: 0(6.61821e-07) 28s
77196 1.4061135156e+11 Pr: 6102(6.24493e+10); Du: 0(9.39584e-07) 33s
83271 1.4243496445e+11 Pr: 702(1.13367e+09); Du: 0(1.24396e-06) 38s
86012 1.4282398309e+11 Pr: 0(0); Du: 0(3.41061e-13) 40s
Solving the original LP from the solution after postsolve
Model name : linopy-problem-yiutdf9u
Model status : Optimal
Simplex iterations: 86012
Objective value : 1.4282398309e+11
Relative P-D gap : 1.3247703942e-14
HiGHS run time : 40.13
Writing the solution to /tmp/linopy-solve-sc2o3n6u.sol
INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 43811 primals, 94181 duals
Objective: 1.43e+11
Solver model: available
Solver message: Optimal
INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-ext-p-lower, Generator-ext-p-upper, Link-fix-p-lower, Link-fix-p-upper, Link-ext-p-lower, Link-ext-p-upper, Store-fix-e-lower, Store-fix-e-upper, Store-ext-e-lower, Store-ext-e-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.
[43]:
('ok', 'optimal')
[44]:
n.statistics()
[44]:
Optimal Capacity | Installed Capacity | Supply | Withdrawal | Energy Balance | Transmission | Capacity Factor | Curtailment | Capital Expenditure | Operational Expenditure | Revenue | Market Value | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Generator | offwind | 2.815849e+05 | 0.0 | 8.833137e+08 | 0.000000e+00 | 8.833137e+08 | 0.000000e+00 | 0.358098 | 1.157736e+07 | 4.915240e+10 | 1.872625e+07 | 4.917112e+10 | 55.666663 |
onwind | 1.027945e+05 | 0.0 | 1.417214e+08 | 0.000000e+00 | 1.417214e+08 | 0.000000e+00 | 0.157384 | 4.354569e+07 | 1.044846e+10 | 2.024632e+08 | 1.065092e+10 | 75.153954 | |
solar | 4.778360e+05 | 0.0 | 5.198227e+08 | 0.000000e+00 | 5.198227e+08 | 0.000000e+00 | 0.124186 | 0.000000e+00 | 2.453537e+10 | 5.510121e+06 | 2.454088e+10 | 47.210086 | |
Link | CHP | 2.070308e+05 | 0.0 | 0.000000e+00 | 1.000000e+08 | -1.000000e+08 | 1.000000e+08 | 0.055139 | 0.000000e+00 | 8.281231e+09 | 0.000000e+00 | -2.471433e+10 | NaN |
EV charger | 4.400000e+05 | 440000.0 | 0.000000e+00 | 2.292063e+08 | -2.292063e+08 | 0.000000e+00 | 0.059466 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | -1.503501e+10 | NaN | |
V2G | 4.400000e+05 | 440000.0 | 0.000000e+00 | 8.200809e+07 | -8.200809e+07 | 0.000000e+00 | 0.021276 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | -8.165188e+09 | NaN | |
electrolysis | 1.770623e+05 | 0.0 | 0.000000e+00 | 7.069242e+08 | -7.069242e+08 | 7.069242e+08 | 0.455767 | 0.000000e+00 | 8.853113e+09 | 0.000000e+00 | -1.701664e+10 | NaN | |
fuel cell | 1.372234e+04 | 0.0 | 0.000000e+00 | 1.592122e+07 | -1.592122e+07 | 1.592122e+07 | 0.132448 | 0.000000e+00 | 1.646680e+09 | 0.000000e+00 | -1.862079e+09 | NaN | |
heat pump | 5.393521e+04 | 0.0 | 0.000000e+00 | 2.201725e+08 | -2.201725e+08 | 2.201725e+08 | 0.466001 | 0.000000e+00 | 1.618056e+10 | 0.000000e+00 | -3.124643e+10 | NaN | |
resistive heater | 7.600678e+04 | 0.0 | 0.000000e+00 | 3.139689e+07 | -3.139689e+07 | 3.139689e+07 | 0.047155 | 0.000000e+00 | 7.600678e+08 | 0.000000e+00 | -4.647582e+09 | NaN | |
Load | - | 0.000000e+00 | 0.0 | 0.000000e+00 | 4.789257e+08 | -4.789257e+08 | 0.000000e+00 | NaN | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | -4.851206e+10 | NaN |
EV demand | 0.000000e+00 | 0.0 | 0.000000e+00 | 1.242776e+08 | -1.242776e+08 | 0.000000e+00 | NaN | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | -1.249521e+10 | NaN | |
heat | 0.000000e+00 | 0.0 | 0.000000e+00 | 5.936692e+08 | -5.936692e+08 | 0.000000e+00 | NaN | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | -6.540936e+10 | NaN | |
hydrogen | 0.000000e+00 | 0.0 | 0.000000e+00 | 4.789257e+08 | -4.789257e+08 | 0.000000e+00 | NaN | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | -3.174707e+10 | NaN | |
Store | EV battery | 1.000000e+06 | 1000000.0 | 1.361451e+08 | 1.361451e+08 | 0.000000e+00 | 0.000000e+00 | 0.721029 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 5.625384e+09 | 41.319041 |
gas storage | 1.000000e+08 | 100000000.0 | 1.000000e+08 | 0.000000e+00 | 1.000000e+08 | 0.000000e+00 | 0.266124 | 0.000000e+00 | 0.000000e+00 | 1.500000e+10 | 2.471433e+10 | 247.143278 | |
hydrogen storage | 5.528145e+07 | 0.0 | 2.260205e+08 | 2.260205e+08 | 0.000000e+00 | 0.000000e+00 | 0.510590 | 0.000000e+00 | 7.739403e+09 | 0.000000e+00 | 7.739403e+09 | 34.242033 |
[45]:
n.statistics()["Capital Expenditure"].div(1e9).sort_values().dropna().plot.bar(
ylabel="bn€/a", cmap="tab20c", figsize=(7, 3)
)
[45]:
<Axes: ylabel='bn€/a'>

[ ]: