Post · 11 min read· pp. 27–38

Meshed AC-DC Networks

This example demonstrates how to optimise meshed AC-DC networks in PyPSA. The example has a 3-node AC network coupled via AC-DC converters to a 3-node DC network. There is also a single point-to-point DC connection using the Link component.

In [2]:

Copied! diagram

ipynb
import pypsa

n = pypsa.examples.ac_dc_meshed()
n.links.loc["Norwich Converter", "p_nom_extendable"] = False

import pypsa n = pypsa.examples.ac_dc_meshed() n.links.loc["Norwich Converter", "p_nom_extendable"] = False

text
/tmp/ipykernel_2633/2868712186.py:3: 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.ac_dc_meshed()
text
INFO:pypsa.network.io:Imported network 'AC-DC-Meshed' has buses, carriers, generators, global_constraints, lines, links, loads

In [3]:

Copied! diagram

ipynb
line_color = n.lines.bus0.map(n.buses.carrier).map(
    lambda ct: "r" if ct == "DC" else "b"
)

n.plot(
    line_color=line_color,
    link_color="c",
    title="AC (blue) - DC (red) - P2P-DC (cyan)",
    jitter=0.4,
)

line_color = n.lines.bus0.map(n.buses.carrier).map( lambda ct: "r" if ct == "DC" else "b" ) n.plot( line_color=line_color, link_color="c", title="AC (blue) - DC (red) - P2P-DC (cyan)", jitter=0.4, )

Out[3]:

text
{'nodes': {'Bus': <matplotlib.collections.PatchCollection at 0x7a29a17daa50>},
 'branches': {'Link': <matplotlib.collections.LineCollection at 0x7a29a17daba0>,
  'Line': <matplotlib.collections.LineCollection at 0x7a29a18c0410>},
 'flows': {}}
text
INFO:shapefile:File length is exactly the declared header and records size, but misses the EOF byte. Last byte is 0x1a, but that value could also be from the last record's data. Note: In previous versions of PyShp, Writers did not output a .dbf EOF byte so no warning will be given.  Set strict=True to raise an Exception for this instead. 

No description has been provided for this image

We inspect the topology of the network. Therefore, use n.determine_network_topology() and inspect the subnetworks in n.sub_networks.

In [4]:

Copied! diagram

ipynb
n.determine_network_topology()
n.sub_networks["n_branches"] = [len(sn.branches()) for sn in n.sub_networks.obj]
n.sub_networks["n_buses"] = [
    len(sn.components.buses.static) for sn in n.sub_networks.obj
]

n.sub_networks

n.determine_network_topology() n.sub_networks["n_branches"] = [len(sn.branches()) for sn in n.sub_networks.obj] n.sub_networks["n_buses"] = [ len(sn.components.buses.static) for sn in n.sub_networks.obj ] n.sub_networks

Out[4]:

carrierslack_busobjn_branchesn_buses
name
0ACManchester<pypsa.SubNetwork object at 0x7a29a16...33
1DCNorwich DC<pypsa.SubNetwork object at 0x7a29a35...33
2ACFrankfurt<pypsa.SubNetwork object at 0x7a29a16...12
3ACNorway<pypsa.SubNetwork object at 0x7a29a17...01

The network covers 10 time steps. These are given by the snapshots attribute.

In [5]:

Copied! diagram

ipynb
n.snapshots

n.snapshots

Out[5]:

text
DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)

There are 6 generators in the network, 3 wind and 3 gas. All are attached to AC buses:

In [6]:

Copied! diagram

ipynb
n.generators

n.generators

Out[6]:

buscontroltypep_nomp_nom_modp_nom_extendablep_nom_minp_nom_maxp_nom_setp_min_pu...down_time_beforeramp_limit_upramp_limit_downramp_limit_start_upramp_limit_shut_downramp_up_costramp_down_costweightp_nom_optcapital_cost_piecewise_opt
name
Manchester WindManchesterSlack80.00.0True100.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0
Manchester GasManchesterPQ50000.00.0True0.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0
Norway WindNorwaySlack100.00.0True100.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0
Norway GasNorwayPQ20000.00.0True0.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0
Frankfurt WindFrankfurtSlack110.00.0True100.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0
Frankfurt GasFrankfurtPQ80000.00.0True0.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0

6 rows × 49 columns

We see that the generators have different capital and marginal costs. All of them have a p_nom_extendable set to True, meaning that capacities can be extended in the optimisation. The wind generators have a per unit limit for each time step, given by the weather potentials at the site.

In [7]:

Copied! diagram

ipynb
n.generators_t.p_max_pu.plot()

n.generators_t.p_max_pu.plot()

Out[7]:

text
<Axes: xlabel='snapshot'>

No description has been provided for this image

Alright now we know how the network looks like, where the generators and lines are. Now, let's perform a optimization of the operation and capacities.

In [8]:

Copied! diagram

ipynb
n.optimize();

n.optimize();

text
/tmp/ipykernel_2633/1450685117.py:1: 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();
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='name')
text
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.08s
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-x0q2t5qy.lp
text
Reading time = 0.00 seconds
text
obj: 467 rows, 187 columns, 986 nonzeros
text
Set parameter LogToConsole to value 0
text
INFO:linopy.constants: Optimization successful: 
Status: ok
Termination condition: optimal
Solution: 187 primals, 467 duals
Objective: -3.47e+06
Solver: gurobi
Runtime: 0.00s
Dual bound: -3.47e+06
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, Line-ext-s-lower, Line-ext-s-upper, Link-fix-p-lower, Link-fix-p-upper, Link-ext-p-lower, Link-ext-p-upper, Kirchhoff-Voltage-Law were not assigned to the network.

The objective is given by:

In [9]:

Copied! diagram

ipynb
n.objective

n.objective

Out[9]:

text
-3474094.1308162357

Why is this number negative? It considers the starting point of the optimisation, thus the existent capacities given by n.generators.p_nom are taken into account.

The real system cost are given by

In [10]:

Copied! diagram

ipynb
n.objective + n.objective_constant

n.objective + n.objective_constant

Out[10]:

text
18440973.387462907

The optimal capacities are given by p_nom_opt for generators, links and storages and s_nom_opt for lines.

Let's look how the optimal capacities for the generators look like.

In [11]:

Copied! diagram

ipynb
n.generators.p_nom_opt.div(1e3).round(2).sort_values()

n.generators.p_nom_opt.div(1e3).round(2).sort_values()

Out[11]:

text
name
Manchester Gas     0.00
Norway Gas         0.00
Frankfurt Gas      0.98
Norway Wind        1.53
Frankfurt Wind     1.67
Manchester Wind    4.09
Name: p_nom_opt, dtype: float64

Their production is again given as a time-series in n.generators_t.

In [12]:

Copied! diagram

ipynb
n.generators_t.p.div(1e3).plot.area(stacked=True, lw=0, ylabel="GW")

n.generators_t.p.div(1e3).plot.area(stacked=True, lw=0, ylabel="GW")

Out[12]:

text
<Axes: xlabel='snapshot', ylabel='GW'>

No description has been provided for this image

What are the locational marginal prices in the network? From the optimisation these are given for each bus and snapshot.

In [13]:

Copied! diagram

ipynb
n.buses_t.marginal_price.mean(axis=1).plot(figsize=(8, 3), ylabel="€/MWh")

n.buses_t.marginal_price.mean(axis=1).plot(figsize=(8, 3), ylabel="€/MWh")

Out[13]:

text
<Axes: xlabel='snapshot', ylabel='€/MWh'>

No description has been provided for this image

We can inspect further quantities as the active power of AC-DC converters and HVDC link.

In [14]:

Copied! diagram

ipynb
n.links_t.p0.round(2)

n.links_t.p0.round(2)

Out[14]:

nameNorwich ConverterNorway ConverterBremen ConverterDC link
snapshot
2015-01-01 00:00:00-475.91674.58-198.67-92.93
2015-01-01 01:00:00315.07-116.73-198.34-318.00
2015-01-01 02:00:00350.76581.97-932.73-318.00
2015-01-01 03:00:00-721.77272.56449.21318.00
2015-01-01 04:00:00317.37-79.75-237.62-318.00
2015-01-01 05:00:00-62.83-494.20557.03131.58
2015-01-01 06:00:00900.00-257.52-642.48318.00
2015-01-01 07:00:00123.68971.92-1095.60-86.86
2015-01-01 08:00:00244.72850.88-1095.60318.00
2015-01-01 09:00:00441.61-86.85-354.76294.73

In [15]:

Copied! diagram

ipynb
n.lines_t.p0.round(2)

n.lines_t.p0.round(2)

Out[15]:

name0123456
snapshot
2015-01-01 00:00:0041.82-75.76129.66-346.25328.34-15.31-534.34
2015-01-01 01:00:00-449.46787.04-211.27103.80-12.93209.36-823.21
2015-01-01 02:00:00-181.27520.56-505.91-155.15426.82-248.68173.90
2015-01-01 03:00:00-151.89128.06482.03-239.7432.82296.87-743.79
2015-01-01 04:00:00-73.21295.42-227.2090.1710.42-240.11-883.82
2015-01-01 05:00:00-669.721122.86238.90176.07-318.13393.72-1030.85
2015-01-01 06:00:00-661.291378.42-632.37267.6310.11-53.45319.39
2015-01-01 07:00:00-383.77540.91-469.93-346.25625.67393.72600.73
2015-01-01 08:00:00-778.281444.07-522.12-277.40573.48229.29501.35
2015-01-01 09:00:00-528.89836.25-325.31116.3029.44393.72-248.57

...or the active power injection per bus.

In [16]:

Copied! diagram

ipynb
n.buses_t.p.round(2)

n.buses_t.p.round(2)

Out[16]:

nameLondonNorwichNorwich DCManchesterBremenBremen DCFrankfurtNorwayNorway DC
snapshot
2015-01-01 00:00:0057.1360.45-475.91-117.58-534.34-198.67534.340.0674.58
2015-01-01 01:00:00-658.83-577.67315.071236.50-823.21-198.34823.210.0-116.73
2015-01-01 02:00:0067.41-769.24350.76701.83173.90-932.73-173.900.0581.97
2015-01-01 03:00:00-448.75168.81-721.77279.94-743.79449.21743.790.0272.56
2015-01-01 04:00:00166.90-535.53317.37368.63-883.82-237.62883.820.0-79.75
2015-01-01 05:00:00-1063.44-729.15-62.831792.58-1030.85557.031030.850.0-494.20
2015-01-01 06:00:00-607.85-1431.87900.002039.72319.39-642.48-319.390.0-257.52
2015-01-01 07:00:00-777.48-147.19123.68924.68600.73-1095.60-600.730.0971.92
2015-01-01 08:00:00-1007.58-1214.77244.722222.35501.35-1095.60-501.350.0850.88
2015-01-01 09:00:00-922.61-442.53441.611365.14-248.57-354.76248.570.0-86.85

diagram 2026-08-05

diagram 2020-02-28