Meshed AC-DC example

This Jupyter Notebook is also available to download at: http://www.pypsa.org/examples/ac-dc-lopf.ipynb.

This 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 using the Link component.

The data files for this example are in the examples folder of the github repository: https://github.com/PyPSA/PyPSA.

In [1]:
#make the code as Python 3 compatible as possible
from __future__ import print_function, division, absolute_import

import pypsa

import numpy as np

import pandas as pd

import os

import matplotlib.pyplot as plt

import cartopy.crs as ccrs

%matplotlib inline
In [2]:
#You may have to adjust this path to where 
#you downloaded the github repository
#https://github.com/PyPSA/PyPSA

csv_folder_name = (os.path.dirname(pypsa.__file__) 
                   + "/../examples/ac-dc-meshed/ac-dc-data/")

network = pypsa.Network(csv_folder_name)
WARNING:pypsa.io:
Importing PyPSA from older version of PyPSA than current version 0.15.0.
Please read the release notes at https://pypsa.org/doc/release_notes.html
carefully to prepare your network for import.

INFO:pypsa.io:Imported network  has buses, carriers, generators, global_constraints, lines, links, loads
In [3]:
#get current type (AC or DC) of the lines from the buses
lines_current_type = pd.merge(network.lines,network.buses,how="left",left_on="bus0",right_index=True,suffixes=("","bus"))["carrier"]
lines_current_type
Out[3]:
name
0    AC
1    AC
2    DC
3    DC
4    DC
5    AC
6    AC
Name: carrier, dtype: object
In [4]:
fig,ax = plt.subplots(1,1,subplot_kw={"projection":ccrs.PlateCarree()})

fig.set_size_inches(5,4)

network.plot(ax=ax,line_colors=lines_current_type.map(lambda ct: "r" if ct=="DC" else "b"))

ax.set_title('Mixed AC (blue) - DC (red) network - DC (cyan) P2P')
Out[4]:
Text(0.5, 1.0, 'Mixed AC (blue) - DC (red) network - DC (cyan) P2P')
In [5]:
fig.tight_layout()
#fig.savefig("meshed-ac-dc.png")
In [6]:
network.links.loc["Norwich Converter",'p_nom_extendable']=False
In [7]:
#perform linear OPF

#compute optimal caps
network.lopf(network.snapshots)
INFO:pypsa.pf:Slack bus for sub-network 0 is Manchester
WARNING:pypsa.pf:No generators in sub-network 1, better hope power is already balanced
INFO:pypsa.pf:Slack bus for sub-network 1 is Norwich DC
INFO:pypsa.pf:Slack bus for sub-network 2 is Frankfurt
INFO:pypsa.pf:Slack bus for sub-network 3 is Norway
INFO:pypsa.opf:Performed preliminary steps
INFO:pypsa.opf:Building pyomo model using `angles` formulation
INFO:pypsa.opf:Solving model using glpk
INFO:pypsa.opf:Optimization successful
# ==========================================================
# = Solver Results                                         =
# ==========================================================
# ----------------------------------------------------------
#   Problem Information
# ----------------------------------------------------------
Problem: 
- Name: unknown
  Lower bound: -3474094.13081625
  Upper bound: -3474094.13081625
  Number of objectives: 1
  Number of constraints: 542
  Number of variables: 277
  Number of nonzeros: 1161
  Sense: minimize
# ----------------------------------------------------------
#   Solver Information
# ----------------------------------------------------------
Solver: 
- Status: ok
  Termination condition: optimal
  Statistics: 
    Branch and bound: 
      Number of bounded subproblems: 0
      Number of created subproblems: 0
  Error rc: 0
  Time: 0.026964426040649414
# ----------------------------------------------------------
#   Solution Information
# ----------------------------------------------------------
Solution: 
- number of solutions: 0
  number of solutions displayed: 0
Out[7]:
('ok', 'optimal')
In [8]:
network.sub_networks["n_branches"] = [len(sn.branches()) for sn in network.sub_networks.obj]
network.sub_networks["n_buses"] = [len(sn.buses()) for sn in network.sub_networks.obj]

network.sub_networks
Out[8]:
attribute carrier slack_bus obj n_branches n_buses
0 AC Manchester SubNetwork 0 3 3
1 DC Norwich DC SubNetwork 1 3 3
2 AC Frankfurt SubNetwork 2 1 2
3 AC Norway SubNetwork 3 0 1
In [9]:
#Plot active power of AC-DC converters and HVDC link
network.links_t.p0.plot()
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f786cd30048>
In [10]:
network.lines_t.p0.plot()
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f788cb5d9e8>
In [11]:
network.buses_t.p.plot()
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f788cae6b70>

Branch (Line and Link) shadow prices

In [12]:
network.lines_t.mu_upper
Out[12]:
name 0 1 2 3 4 5 6
name
2015-01-01 00:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 -0.000000e+00 -0.0
2015-01-01 01:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 5.207157e-12 -0.0
2015-01-01 02:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 -0.000000e+00 -0.0
2015-01-01 03:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 -0.000000e+00 -0.0
2015-01-01 04:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 -0.000000e+00 -0.0
2015-01-01 05:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 9.896574e-13 -0.0
2015-01-01 06:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 -0.000000e+00 -0.0
2015-01-01 07:00:00 -0.0 -0.000000 -0.0 -0.0 0.06243 2.185245e-02 -0.0
2015-01-01 08:00:00 -0.0 0.133492 -0.0 -0.0 -0.00000 -0.000000e+00 -0.0
2015-01-01 09:00:00 -0.0 -0.000000 -0.0 -0.0 -0.00000 1.393866e-11 -0.0
In [13]:
network.lines_t.mu_lower
Out[13]:
name 0 1 2 3 4 5 6
name
2015-01-01 00:00:00 0.000000 0.0 0.000000 0.000000 0.0 0.0 0.0
2015-01-01 01:00:00 0.000000 0.0 0.000000 0.000000 0.0 0.0 0.0
2015-01-01 02:00:00 0.000000 0.0 0.000000 0.000000 0.0 0.0 0.0
2015-01-01 03:00:00 0.000000 0.0 0.000000 0.000000 0.0 0.0 0.0
2015-01-01 04:00:00 0.000000 0.0 0.000000 0.000000 0.0 0.0 0.0
2015-01-01 05:00:00 0.000000 0.0 0.000000 0.000000 0.0 0.0 0.2
2015-01-01 06:00:00 0.000000 0.0 0.008673 0.000000 0.0 0.0 0.0
2015-01-01 07:00:00 0.000000 0.0 0.000000 0.129126 0.0 0.0 0.0
2015-01-01 08:00:00 0.136716 0.0 0.000000 0.000000 0.0 0.0 0.0
2015-01-01 09:00:00 0.000000 0.0 0.000000 0.000000 0.0 0.0 0.0
In [14]:
network.links_t.mu_upper
Out[14]:
name Norwich Converter Norway Converter Bremen Converter DC link
name
2015-01-01 00:00:00 -0.00000 -0.000000 -0.0 -0.000000e+00
2015-01-01 01:00:00 -0.00000 -0.000000 -0.0 -0.000000e+00
2015-01-01 02:00:00 -0.00000 -0.000000 -0.0 -3.165263e-12
2015-01-01 03:00:00 -0.00000 -0.000000 -0.0 -0.000000e+00
2015-01-01 04:00:00 -0.00000 -0.000000 -0.0 -0.000000e+00
2015-01-01 05:00:00 -0.00000 -0.000000 -0.0 -0.000000e+00
2015-01-01 06:00:00 0.78709 -0.000000 -0.0 7.941279e-01
2015-01-01 07:00:00 -0.00000 0.222222 -0.0 -0.000000e+00
2015-01-01 08:00:00 -0.00000 -0.000000 -0.0 1.797990e-01
2015-01-01 09:00:00 -0.00000 -0.000000 -0.0 -0.000000e+00
In [15]:
network.links_t.mu_lower
Out[15]:
name Norwich Converter Norway Converter Bremen Converter DC link
name
2015-01-01 00:00:00 0.0 0.0 0.000000 0.000000e+00
2015-01-01 01:00:00 0.0 0.0 0.000000 0.000000e+00
2015-01-01 02:00:00 0.0 0.0 0.000000 0.000000e+00
2015-01-01 03:00:00 0.0 0.0 0.000000 0.000000e+00
2015-01-01 04:00:00 0.0 0.0 0.000000 7.694507e-13
2015-01-01 05:00:00 0.0 0.0 0.000000 0.000000e+00
2015-01-01 06:00:00 0.0 0.0 0.000000 0.000000e+00
2015-01-01 07:00:00 0.0 0.0 0.030773 0.000000e+00
2015-01-01 08:00:00 0.0 0.0 0.180338 0.000000e+00
2015-01-01 09:00:00 0.0 0.0 0.000000 0.000000e+00