Post · 12 min read· pp. 294–305

Extraction-Condensing CHP

This example demonstrates how to model an extraction-condensing Combined Heat and Power (CHP) plant with flexible heat-power ratios. For an example of a CHP plant with a fixed heat-power ratio, see the backpressure CHP example.

In this example, a location has an electric, gas, and heat bus. The primary energy source is wind power, which can be converted to gas. The gas can be stored to convert into electricity and/or heat when needed, using either a boiler or a CHP unit.

In [2]:

Copied! diagram

ipynb
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

import pypsa

import matplotlib.pyplot as plt import numpy as np import pandas as pd import pypsa

CHP parametrisation

The setup roughly follows Grohnheit (1993). We define the ratio between maximum heat and power output of the CHP unit as nom_r, the backpressure limit as c_m, and the marginal loss for each additional generation of heat as c_v.

In [3]:

Copied! diagram

ipynb
nom_r = 1.0
c_m = 0.75
c_v = 0.15

nom_r = 1.0 c_m = 0.75 c_v = 0.15

The feasible operational space of the CHP unit is shown in the graph below:

In [4]:

Copied! diagram

ipynb
fig, ax = plt.subplots()

t = 0.01
ph = np.arange(0, 1.0001, t)

ax.plot(ph, c_m * ph, color="k")
ax.set_xlabel("P_heat_out")
ax.set_ylabel("P_elec_out")
ax.grid(True)

ax.set_xlim([0, 1.1])
ax.set_ylim([0, 1.1])
ax.text(0.1, 0.7, "Allowed output", color="gray")
ax.plot(ph, 1 - c_v * ph, color="k")

for i in range(1, 10):
    k = 0.1 * i
    x = np.arange(0, k / (c_m + c_v), t)
    ax.plot(x, k - c_v * x, color="orange", linestyle="--", linewidth=0.75)

ax.text(0.05, 0.41, "iso-fuel-lines", color="orange", rotation=-7)
ax.fill_between(ph, c_m * ph, 1 - c_v * ph, facecolor="#ddd")

fig, ax = plt.subplots() t = 0.01 ph = np.arange(0, 1.0001, t) ax.plot(ph, c_m * ph, color="k") ax.set_xlabel("P_heat_out") ax.set_ylabel("P_elec_out") ax.grid(True) ax.set_xlim([0, 1.1]) ax.set_ylim([0, 1.1]) ax.text(0.1, 0.7, "Allowed output", color="gray") ax.plot(ph, 1 - c_v * ph, color="k") for i in range(1, 10): k = 0.1 * i x = np.arange(0, k / (c_m + c_v), t) ax.plot(x, k - c_v * x, color="orange", linestyle="--", linewidth=0.75) ax.text(0.05, 0.41, "iso-fuel-lines", color="orange", rotation=-7) ax.fill_between(ph, c_m * ph, 1 - c_v * ph, facecolor="#ddd")

Out[4]:

text
<matplotlib.collections.FillBetweenPolyCollection at 0x7efd276f9400>

No description has been provided for this image

Optimisation

First, we add the power and gas sectors with a wind generator, a power-to-gas unit, a gas storage unit, and the turbine of the CHP unit:

In [5]:

Copied! diagram

ipynb
n = pypsa.Network()
n.set_snapshots(pd.date_range("2025-01-01 00:00", "2025-01-01 03:00", freq="h"))

n.add("Bus", "0 power", carrier="AC")
n.add("Bus", "0 gas", carrier="gas")

n.add("Carrier", ["wind", "gas"])

n.add(
    "Generator",
    "wind turbine",
    bus="0 power",
    carrier="wind",
    p_nom_extendable=True,
    p_max_pu=[0.0, 0.2, 0.7, 0.4],
    capital_cost=1000,
)

n.add("Load", "load", bus="0 power", p_set=5)

n.add(
    "Link",
    "power-to-gas",
    bus0="0 power",
    bus1="0 gas",
    efficiency=0.6,
    capital_cost=1000,
    p_nom_extendable=True,
)

n.add(
    "Link",
    "generator",
    bus0="0 gas",
    bus1="0 power",
    efficiency=0.468,
    capital_cost=400,
    p_nom_extendable=True,
)

n.add("Store", "gas depot", bus="0 gas", e_cyclic=True, e_nom=1000);

n = pypsa.Network() n.set_snapshots(pd.date_range("2025-01-01 00:00", "2025-01-01 03:00", freq="h")) n.add("Bus", "0 power", carrier="AC") n.add("Bus", "0 gas", carrier="gas") n.add("Carrier", ["wind", "gas"]) n.add( "Generator", "wind turbine", bus="0 power", carrier="wind", p_nom_extendable=True, p_max_pu=[0.0, 0.2, 0.7, 0.4], capital_cost=1000, ) n.add("Load", "load", bus="0 power", p_set=5) n.add( "Link", "power-to-gas", bus0="0 power", bus1="0 gas", efficiency=0.6, capital_cost=1000, p_nom_extendable=True, ) n.add( "Link", "generator", bus0="0 gas", bus1="0 power", efficiency=0.468, capital_cost=400, p_nom_extendable=True, ) n.add("Store", "gas depot", bus="0 gas", e_cyclic=True, e_nom=1000);

text
/tmp/ipykernel_8309/703670829.py:1: 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.Network()

Next, we add the heat sector with the boiler of the CHP unit.

In [6]:

Copied! diagram

ipynb
n.add("Bus", "0 heat", carrier="heat")

n.add("Carrier", "heat")

n.add("Load", "heat load", bus="0 heat", p_set=10)

n.add(
    "Link",
    "boiler",
    bus0="0 gas",
    bus1="0 heat",
    efficiency=0.9,
    capital_cost=300,
    p_nom_extendable=True,
)

n.add("Store", "water tank", bus="0 heat", e_cyclic=True, e_nom_extendable=True)

n.add("Bus", "0 heat", carrier="heat") n.add("Carrier", "heat") n.add("Load", "heat load", bus="0 heat", p_set=10) n.add( "Link", "boiler", bus0="0 gas", bus1="0 heat", efficiency=0.9, capital_cost=300, p_nom_extendable=True, ) n.add("Store", "water tank", bus="0 heat", e_cyclic=True, e_nom_extendable=True)

Next, we need to add some constraints that ensure that combination of turbine and boiler output of the CHP is operationally feasible. This is done by implementing the operational space shown in the graph above with a set of linear constraints.

In [7]:

Copied! diagram

ipynb
# Guarantees ISO fuel lines, i.e. fuel consumption p_b0 + p_g0 = constant along p_g1 + c_v p_b1 = constant (b=boiler, g=generator)
n.links.at["boiler", "efficiency"] = n.links.at["generator", "efficiency"] / c_v
boiler_eff = float(n.links.at["boiler", "efficiency"])
generator_eff = float(n.links.at["generator", "efficiency"])

m = n.optimize.create_model()

p = m.variables["Link-p"]
p_nom = m.variables["Link-p_nom"]

# Guarantees heat output and electric output nominal powers are proportional
m.add_constraints(
    generator_eff * nom_r * p_nom.loc["generator"] - boiler_eff * p_nom.loc["boiler"]
    == 0,
    name="heat-power output proportionality",
)

# Guarantees c_m p_b1 <= p_g1
m.add_constraints(
    p.loc[:, "boiler"] * c_m * boiler_eff - p.loc[:, "generator"] * generator_eff <= 0,
    name="backpressure",
)

# Guarantees p_g1 +c_v p_b1 <= p_g1_nom
m.add_constraints(
    p.loc[:, "boiler"] + p.loc[:, "generator"] - p_nom.loc["generator"] <= 0,
    name="top_iso_fuel_line",
)

n.optimize.solve_model(log_to_console=False)

Guarantees ISO fuel lines, i.e. fuel consumption p_b0 + p_g0 = constant along p_g1 + c_v p_b1 = constant (b=boiler, g=generator)

n.links.at["boiler", "efficiency"] = n.links.at["generator", "efficiency"] / c_v boiler_eff = float(n.links.at["boiler", "efficiency"]) generator_eff = float(n.links.at["generator", "efficiency"]) m = n.optimize.create_model() p = m.variables["Link-p"] p_nom = m.variables["Link-p_nom"]

Guarantees heat output and electric output nominal powers are proportional

m.add_constraints( generator_eff * nom_r * p_nom.loc["generator"] - boiler_eff * p_nom.loc["boiler"] == 0, name="heat-power output proportionality", )

Guarantees c_m p_b1 <= p_g1

m.add_constraints( p.loc[:, "boiler"] * c_m * boiler_eff - p.loc[:, "generator"] * generator_eff <= 0, name="backpressure", )

Guarantees p_g1 +c_v p_b1 <= p_g1_nom

m.add_constraints( p.loc[:, "boiler"] + p.loc[:, "generator"] - p_nom.loc["generator"] <= 0, name="top_iso_fuel_line", ) n.optimize.solve_model(log_to_console=False)

text
WARNING:pypsa.consistency:The following buses have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['0 power'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following links have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['power-to-gas'], dtype='object', name='name')
text
/tmp/ipykernel_8309/1779618540.py:6: 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.
  m = n.optimize.create_model()
text
/tmp/ipykernel_8309/1779618540.py:13: LinopySemanticsWarning: Auxiliary coordinate 'name' was conflicting across operands and silently dropped by legacy (xarray's default). Under v1 this raises ValueError.
  Values:    left='generator', right='boiler'
  Resolve:   `.drop_vars('name')`
             `.assign_coords(name=...)` to relabel one side
             or `.isel(..., drop=True)` if a scalar isel introduced it.
  Opt in:    linopy.options['semantics'] = 'v1'
  Silence:   warnings.filterwarnings('ignore', category=LinopySemanticsWarning)
  generator_eff * nom_r * p_nom.loc["generator"] - boiler_eff * p_nom.loc["boiler"]
/tmp/ipykernel_8309/1779618540.py:20: LinopySemanticsWarning: Auxiliary coordinate 'name' was conflicting across operands and silently dropped by legacy (xarray's default). Under v1 this raises ValueError.
  Values:    left='boiler', right='generator'
  Resolve:   `.drop_vars('name')`
             `.assign_coords(name=...)` to relabel one side
             or `.isel(..., drop=True)` if a scalar isel introduced it.
  Opt in:    linopy.options['semantics'] = 'v1'
  Silence:   warnings.filterwarnings('ignore', category=LinopySemanticsWarning)
  p.loc[:, "boiler"] * c_m * boiler_eff - p.loc[:, "generator"] * generator_eff <= 0,
/tmp/ipykernel_8309/1779618540.py:26: LinopySemanticsWarning: Auxiliary coordinate 'name' was conflicting across operands and silently dropped by legacy (xarray's default). Under v1 this raises ValueError.
  Values:    left='boiler', right='generator'
  Resolve:   `.drop_vars('name')`
             `.assign_coords(name=...)` to relabel one side
             or `.isel(..., drop=True)` if a scalar isel introduced it.
  Opt in:    linopy.options['semantics'] = 'v1'
  Silence:   warnings.filterwarnings('ignore', category=LinopySemanticsWarning)
  p.loc[:, "boiler"] + p.loc[:, "generator"] - p_nom.loc["generator"] <= 0,
/tmp/ipykernel_8309/1779618540.py:26: LinopySemanticsWarning: Auxiliary coordinate 'name' was conflicting across operands and silently dropped by legacy (xarray's default). Under v1 this raises ValueError.
  Values:    left='boiler', right='generator'
  Resolve:   `.drop_vars('name')`
             `.assign_coords(name=...)` to relabel one side
             or `.isel(..., drop=True)` if a scalar isel introduced it.
  Opt in:    linopy.options['semantics'] = 'v1'
  Silence:   warnings.filterwarnings('ignore', category=LinopySemanticsWarning)
  p.loc[:, "boiler"] + p.loc[:, "generator"] - p_nom.loc["generator"] <= 0,
INFO:linopy.model: Solve problem using Gurobi solver
text
INFO:linopy.model:Solver options:
 - log_to_console: False
text
INFO:linopy.io: Writing time: 0.2s
text
Set parameter WLSAccessID
text
Set parameter WLSSecret
text
Set parameter LicenseID to value 2537914
text
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
text
Read LP format model from file /tmp/linopy-problem-8q25lktl.lp
text
Reading time = 0.00 seconds
text
obj: 98 rows, 45 columns, 170 nonzeros
text
Set parameter LogToConsole to value 0
text
INFO:linopy.constants: Optimization successful: 
Status: ok
Termination condition: optimal
Solution: 45 primals, 114 duals
Objective: 1.62e+05
Solver: gurobi
Runtime: 0.00s
Dual bound: 1.62e+05
Solver model: available
Solver message: 2
text
INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-ext-p-lower, Generator-ext-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, Store-fix-p-lower, Store-fix-p-upper, Store-ext-p-lower, Store-ext-p-upper, Store-fix-p_store-lower, Store-fix-p_store-upper, Store-ext-p_store-lower, Store-ext-p_store-upper, Store-energy_balance, heat-power output proportionality, backpressure, top_iso_fuel_line were not assigned to the network.

Out[7]:

text
('ok', 'optimal')

In [8]:

Copied! diagram

ipynb
n.objective

n.objective

Out[8]:

text
162253.99956169192

Results

Let's start by inspecting the optimised conversion capacities:

In [9]:

Copied! diagram

ipynb
n.links.p_nom_opt

n.links.p_nom_opt

Out[9]:

text
name
power-to-gas    58.648915
generator       28.490028
boiler           4.273504
Name: p_nom_opt, dtype: float64

The CHP boiler is dimensioned by the heat demand met in three hours when there is no wind supply. The CHP generator is set by the heat demand.

In [10]:

Copied! diagram

ipynb
display(4 * 10 / 3 / float(n.links.at["boiler", "efficiency"]))
display(28.490028 * 0.15)

display(4 * 10 / 3 / float(n.links.at["boiler", "efficiency"])) display(28.490028 * 0.15)

text
4.273504273504273
text
4.2735042

In [11]:

Copied! diagram

ipynb
n.links_t.p0.round(2)

n.links_t.p0.round(2)

Out[11]:

namepower-to-gasgeneratorboiler
snapshot
2025-01-01 00:00:005.0021.374.27
2025-01-01 01:00:0023.1921.374.27
2025-01-01 02:00:0058.650.000.00
2025-01-01 03:00:0041.3721.374.27

In [12]:

Copied! diagram

ipynb
n.links_t.p1.round(2)

n.links_t.p1.round(2)

Out[12]:

namepower-to-gasgeneratorboiler
snapshot
2025-01-01 00:00:00-3.00-10.0-13.33
2025-01-01 01:00:00-13.91-10.0-13.33
2025-01-01 02:00:00-35.19-0.0-0.00
2025-01-01 03:00:00-24.82-10.0-13.33

In [13]:

Copied! diagram

ipynb
pd.DataFrame({attr: n.stores_t[attr]["gas depot"] for attr in ["p", "e"]}).round(2)

pd.DataFrame({attr: n.stores_t[attr]["gas depot"] for attr in ["p", "e"]}).round(2)

Out[13]:

pe
snapshot
2025-01-01 00:00:0022.6411.73
2025-01-01 01:00:0011.730.00
2025-01-01 02:00:00-35.1935.19
2025-01-01 03:00:000.8234.37

In [14]:

Copied! diagram

ipynb
pd.DataFrame({attr: n.stores_t[attr]["water tank"] for attr in ["p", "e"]}).round(2)

pd.DataFrame({attr: n.stores_t[attr]["water tank"] for attr in ["p", "e"]}).round(2)

Out[14]:

pe
snapshot
2025-01-01 00:00:00-3.336.67
2025-01-01 01:00:00-3.3310.00
2025-01-01 02:00:0010.000.00
2025-01-01 03:00:00-3.333.33

In [15]:

Copied! diagram

ipynb
pd.DataFrame({attr: n.links_t[attr]["boiler"] for attr in ["p0", "p1"]}).round(2)

pd.DataFrame({attr: n.links_t[attr]["boiler"] for attr in ["p0", "p1"]}).round(2)

Out[15]:

p0p1
snapshot
2025-01-01 00:00:004.27-13.33
2025-01-01 01:00:004.27-13.33
2025-01-01 02:00:000.00-0.00
2025-01-01 03:00:004.27-13.33

Finally, let's calculate the overall efficiency of the CHP system.

In [16]:

Copied! diagram

ipynb
eta_elec = n.links.at["generator", "efficiency"]

r = 1 / c_m

# P_h = r*P_e
(1 + r) / ((1 / eta_elec) * (1 + c_v * r))

eta_elec = n.links.at["generator", "efficiency"] r = 1 / c_m

P_h = r*P_e

(1 + r) / ((1 / eta_elec) * (1 + c_v * r))

Out[16]:

text
np.float64(0.9099999999999999)

diagram 2026-11-05

diagram 2020-02-28