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

%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=csv_folder_name)
INFO:pypsa.io:buses.csv found.
INFO:pypsa.io:carriers.csv found.
INFO:pypsa.io:generators.csv found.
INFO:pypsa.io:global_constraints.csv found.
INFO:pypsa.io:lines.csv found.
INFO:pypsa.io:No line_types.csv found.
INFO:pypsa.io:links.csv found.
INFO:pypsa.io:loads.csv found.
INFO:pypsa.io:No shunt_impedances.csv found.
INFO:pypsa.io:No storage_units.csv found.
INFO:pypsa.io:No stores.csv found.
INFO:pypsa.io:No transformers.csv found.
INFO:pypsa.io:No transformer_types.csv found.
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)

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]:
<matplotlib.text.Text at 0x7f2ce9acecc0>
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
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 0x7f2ce61a5828>
In [10]:
network.lines_t.p0.plot()
Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f2ce60915c0>
In [11]:
network.buses_t.p.plot()
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f2ce60a2e48>

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 -4.119758e-14 -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 5.679148e-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.871473e-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.675201e-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 -2.810312e-12
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