Skip to content

Commit

Permalink
Merge branch 'develop' of gitbox:pandapower into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenMeinecke committed Mar 27, 2017
2 parents d79862b + 304e48d commit e1ef688
Show file tree
Hide file tree
Showing 25 changed files with 273 additions and 256 deletions.
2 changes: 0 additions & 2 deletions pandapower/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pandas as pd

from pandapower.auxiliary import *
from pandapower.create import *
from pandapower.diagnostic import *
Expand Down
98 changes: 54 additions & 44 deletions pandapower/build_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,28 @@ def _build_branch_ppc(net, ppc):
if "line" in lookup:
f, t = lookup["line"]
ppc["branch"][f:t, [F_BUS, T_BUS, BR_R, BR_X, BR_B,
BR_STATUS, RATE_A]] = _calc_line_parameter(net, ppc)
BR_STATUS, RATE_A]] = _calc_line_parameter(net, ppc)
if "trafo" in lookup:
f, t = lookup["trafo"]
ppc["branch"][f:t, [F_BUS, T_BUS, BR_R, BR_X, BR_B, TAP, SHIFT, BR_STATUS,
RATE_A]] = _calc_trafo_parameter(net, ppc)
ppc["branch"][f:t, [F_BUS, T_BUS, BR_R, BR_X, BR_B, TAP, SHIFT, BR_STATUS,
RATE_A]] = _calc_trafo_parameter(net, ppc)
if "trafo3w" in lookup:
f, t = lookup["trafo3w"]
ppc["branch"][f:t, [F_BUS, T_BUS, BR_R, BR_X, BR_B, TAP, SHIFT, BR_STATUS, RATE_A]] = \
_calc_trafo3w_parameter(net, ppc)
_calc_trafo3w_parameter(net, ppc)
if "impedance" in lookup:
f, t = lookup["impedance"]
ppc["branch"][f:t, [F_BUS, T_BUS, BR_R, BR_X, 17, 18, BR_STATUS]] = \
_calc_impedance_parameter(net)
_calc_impedance_parameter(net)
if "xward" in lookup:
f,t = lookup["xward"]
f, t = lookup["xward"]
ppc["branch"][f:t, [F_BUS, T_BUS, BR_R, BR_X, BR_STATUS]] = _calc_xward_parameter(net, ppc)

if "switch" in lookup:
f, t = lookup["switch"]
ppc["branch"][f:t, [F_BUS, T_BUS, BR_R]] = _calc_switch_parameter(net, ppc)



def _initialize_branch_lookup(net):
r_switch = net["_options"]["r_switch"]
start = 0
Expand All @@ -83,7 +84,8 @@ def _initialize_branch_lookup(net):
end = start + net._closed_bb_switches.sum()
net._pd2ppc_lookups["branch"]["switch"] = (start, end)
return end



def _calc_trafo3w_parameter(net, ppc):
copy_constraints_to_ppc = net["_options"]["copy_constraints_to_ppc"]
bus_lookup = net["_pd2ppc_lookups"]["bus"]
Expand All @@ -100,6 +102,7 @@ def _calc_trafo3w_parameter(net, ppc):
temp_para[:, 8] = max_load / 100. * trafo_df.sn_kva / 1000.
return temp_para


def _calc_line_parameter(net, ppc):
"""
calculates the line parameter in per unit.
Expand Down Expand Up @@ -129,7 +132,6 @@ def _calc_line_parameter(net, ppc):
t[:, 2] = line["r_ohm_per_km"] * length / baseR / parallel
t[:, 3] = line["x_ohm_per_km"] * length / baseR / parallel
if mode == "sc":
t[:, 4] == 0
if net["_options"]["case"] == "min":
t[:, 2] *= _end_temperature_correction_factor(net)
else:
Expand Down Expand Up @@ -170,6 +172,7 @@ def _calc_trafo_parameter(net, ppc):
temp_para[:, 8] = max_load / 100. * trafo.sn_kva / 1000. * parallel
return temp_para


def _calc_branch_values_from_trafo_df(net, ppc, trafo_df=None):
"""
Calculates the MAT/PYPOWER-branch-attributes from the pandapower trafo dataframe.
Expand Down Expand Up @@ -212,8 +215,8 @@ def _calc_branch_values_from_trafo_df(net, ppc, trafo_df=None):
# 0:r_pu; 1:x_pu; 2:b_pu; 3:tab;
temp_para = np.zeros(shape=(len(trafo_df), 5), dtype=np.complex128)
vn_trafo_hv, vn_trafo_lv, shift = _calc_tap_from_dataframe(net, trafo_df, vn_lv)
ratio = _calc_nominal_ratio_from_dataframe(ppc, trafo_df, vn_trafo_hv, vn_trafo_lv,
bus_lookup)
ratio = _calc_nominal_ratio_from_dataframe(ppc, trafo_df, vn_trafo_hv, vn_trafo_lv,
bus_lookup)
r, x, y = _calc_r_x_y_from_dataframe(net, trafo_df, vn_trafo_lv, vn_lv, net.sn_kva)
temp_para[:, 0] = r / parallel
temp_para[:, 1] = x / parallel
Expand All @@ -235,7 +238,7 @@ def _calc_r_x_y_from_dataframe(net, trafo_df, vn_trafo_lv, vn_lv, sn_kva):
bus_lookup = net._pd2ppc_lookups["bus"]
cmax = net._ppc["bus_sc"][bus_lookup[net.trafo.lv_bus.values], C_MAX]
kt = _transformer_correction_factor(trafo_df.vsc_percent, trafo_df.vscr_percent,
trafo_df.sn_kva, cmax)
trafo_df.sn_kva, cmax)
r *= kt
x *= kt
else:
Expand Down Expand Up @@ -283,18 +286,18 @@ def _calc_y_from_dataframe(trafo_df, vn_lv, vn_trafo_lv, sn_kva):
baseR = np.square(vn_lv) / sn_kva * 1e3

### Calculate subsceptance ###
vnl_squared = trafo_df["vn_lv_kv"].values**2
vnl_squared = trafo_df["vn_lv_kv"].values ** 2
b_real = trafo_df["pfe_kw"].values / (1000. * vnl_squared) * baseR
i0 = trafo_df["i0_percent"].values
pfe = trafo_df["pfe_kw"].values
sn = trafo_df["sn_kva"].values
b_img = (i0 / 100. * sn / 1000.)**2 - (pfe / 1000.)**2
b_img = (i0 / 100. * sn / 1000.) ** 2 - (pfe / 1000.) ** 2

b_img[b_img < 0] = 0
b_img = np.sqrt(b_img) * baseR / vnl_squared
y = - b_real * 1j - b_img * np.sign(i0)
y = - b_real * 1j - b_img * np.sign(i0)
if "lv" in trafo_df["tp_side"].values:
return y / np.square(vn_trafo_lv * vn_lv / trafo_df["vn_lv_kv"].values / vn_lv)
return y / np.square(vn_trafo_lv * vn_lv / trafo_df["vn_lv_kv"].values / vn_lv)
else:
return y

Expand All @@ -319,7 +322,7 @@ def _calc_tap_from_dataframe(net, trafo_df, vn_lv):
mode = net["_options"]["mode"]
# Changing Voltage on high-voltage side
trafo_shift = trafo_df["shift_degree"].values.astype(float) if calculate_voltage_angles else \
np.zeros(len(trafo_df))
np.zeros(len(trafo_df))
vnh = copy.copy(trafo_df["vn_hv_kv"].values.astype(float))
vnl = copy.copy(trafo_df["vn_lv_kv"].values.astype(float))
if mode == "sc":
Expand All @@ -334,7 +337,7 @@ def _calc_tap_from_dataframe(net, trafo_df, vn_lv):
if calculate_voltage_angles and "tp_st_degree" in trafo_df:
ps_os = tap_os & np.isfinite(trafo_df["tp_st_degree"].values)
trafo_shift[ps_os] += tp_diff[ps_os] * trafo_df["tp_st_degree"].values[ps_os]

# Changing Voltage on low-voltage side
tap_us = np.isfinite(trafo_df["tp_pos"].values) & (trafo_df["tp_side"] == "lv").values
if any(tap_us):
Expand All @@ -352,10 +355,10 @@ def _calc_r_x_from_dataframe(trafo_df, vn_lv, vn_trafo_lv, sn_kva):
transformer values
"""
tap_lv = np.square(vn_trafo_lv / vn_lv) * sn_kva * 1e-3 #adjust for low voltage side voltage converter
tap_lv = np.square(vn_trafo_lv / vn_lv) * sn_kva * 1e-3 # adjust for low voltage side voltage converter
z_sc = trafo_df["vsc_percent"].values / 100. / trafo_df.sn_kva.values * 1000. * tap_lv
r_sc = trafo_df["vscr_percent"].values / 100. / trafo_df.sn_kva.values * 1000. *tap_lv
x_sc = np.sqrt(z_sc**2 - r_sc**2)
r_sc = trafo_df["vscr_percent"].values / 100. / trafo_df.sn_kva.values * 1000. * tap_lv
x_sc = np.sqrt(z_sc ** 2 - r_sc ** 2)
return r_sc, x_sc


Expand All @@ -378,13 +381,14 @@ def _calc_nominal_ratio_from_dataframe(ppc, trafo_df, vn_hv_kv, vn_lv_kv, bus_lo
# Calculating tab (trasformer off nominal turns ratio)
tap_rat = vn_hv_kv / vn_lv_kv
nom_rat = get_values(ppc["bus"][:, BASE_KV], trafo_df["hv_bus"].values, bus_lookup) / \
get_values(ppc["bus"][:, BASE_KV], trafo_df["lv_bus"].values, bus_lookup)
get_values(ppc["bus"][:, BASE_KV], trafo_df["lv_bus"].values, bus_lookup)
return tap_rat / nom_rat


def z_br_to_bus(z, s):
return s[0] * np.array([z[0] / min(s[0], s[1]), z[1] /
min(s[1], s[2]), z[2] / min(s[0], s[2])])
min(s[1], s[2]), z[2] / min(s[0], s[2])])


def wye_delta(zbr_n, s):
return .5 * s / s[0] * np.array([(zbr_n[0] + zbr_n[2] - zbr_n[1]),
Expand Down Expand Up @@ -433,24 +437,27 @@ def _trafo_df_from_trafo3w(net):
"i0_percent": ttab.i0_percent, "tp_side": taps[0]["tp_side"],
"tp_mid": taps[0]["tp_mid"], "tp_max": taps[0]["tp_max"],
"tp_min": taps[0]["tp_min"], "tp_pos": taps[0]["tp_pos"],
"tp_st_percent": taps[0]["tp_st_percent"],"parallel": 1,
"tp_st_percent": taps[0]["tp_st_percent"], "parallel": 1,
"in_service": ttab.in_service, "shift_degree": 0, "max_loading_percent": max_load}
trafos2w[i + nr_trafos] = {"hv_bus": ttab.ad_bus, "lv_bus": ttab.mv_bus,
"sn_kva": ttab.sn_mv_kva, "vn_hv_kv": ttab.vn_hv_kv, "vn_lv_kv": ttab.vn_mv_kv,
"vscr_percent": vscr_2w[1], "vsc_percent": vsc_2w[1], "pfe_kw": 0,
"i0_percent": 0, "tp_side": taps[1]["tp_side"],
"tp_mid": taps[1]["tp_mid"], "tp_max": taps[1]["tp_max"],
"tp_min": taps[1]["tp_min"], "tp_pos": taps[1]["tp_pos"],
"tp_st_percent": taps[1]["tp_st_percent"],"parallel": 1,
"in_service": ttab.in_service, "shift_degree": ttab.shift_mv_degree, "max_loading_percent": max_load}
"tp_st_percent": taps[1]["tp_st_percent"], "parallel": 1,
"in_service": ttab.in_service, "shift_degree": ttab.shift_mv_degree,
"max_loading_percent": max_load}
trafos2w[i + 2 * nr_trafos] = {"hv_bus": ttab.ad_bus, "lv_bus": ttab.lv_bus,
"sn_kva": ttab.sn_lv_kva,
"vn_hv_kv": ttab.vn_hv_kv, "vn_lv_kv": ttab.vn_lv_kv, "vscr_percent": vscr_2w[2],
"vsc_percent": vsc_2w[2], "pfe_kw": 0, "i0_percent": 0,
"tp_side": taps[2]["tp_side"], "tp_mid": taps[2]["tp_mid"],
"tp_max": taps[2]["tp_max"], "tp_min": taps[2]["tp_min"],
"tp_pos": taps[2]["tp_pos"], "tp_st_percent": taps[2]["tp_st_percent"],"parallel": 1,
"in_service": ttab.in_service, "shift_degree": ttab.shift_lv_degree, "max_loading_percent": max_load}
"tp_pos": taps[2]["tp_pos"], "tp_st_percent": taps[2]["tp_st_percent"],
"parallel": 1,
"in_service": ttab.in_service, "shift_degree": ttab.shift_lv_degree,
"max_loading_percent": max_load}
i += 1
trafo_df = pd.DataFrame(trafos2w).T
for var in list(tap_variables) + ["i0_percent", "sn_kva", "vsc_percent", "vscr_percent",
Expand Down Expand Up @@ -480,8 +487,8 @@ def _calc_impedance_parameter(net):

def _calc_xward_parameter(net, ppc):
bus_lookup = net["_pd2ppc_lookups"]["bus"]
baseR = np.square(get_values(ppc["bus"][:, BASE_KV], net["xward"]["bus"].values, bus_lookup)) /\
net.sn_kva * 1e3
baseR = np.square(get_values(ppc["bus"][:, BASE_KV], net["xward"]["bus"].values, bus_lookup)) / \
net.sn_kva * 1e3
t = np.zeros(shape=(len(net["xward"].index), 5), dtype=np.complex128)
xw_is = net["_is_elements"]["xward"]
t[:, 0] = bus_lookup[net["xward"]["bus"].values]
Expand Down Expand Up @@ -526,7 +533,7 @@ def _switch_branches(net, ppc):

# opened bus line switches
slidx = (net["switch"]["closed"].values == 0) \
& (net["switch"]["et"].values == "l")
& (net["switch"]["et"].values == "l")

# check if there are multiple opened switches at a line (-> set line out of service)
sw_elem = net['switch'].ix[slidx].element
Expand All @@ -546,17 +553,17 @@ def _switch_branches(net, ppc):
# get from and to buses of these branches
ppc_from = bus_lookup[from_bus]
ppc_to = bus_lookup[to_bus]
ppc_idx = np.in1d(ppc['branch'][:, 0], ppc_from)\
& np.in1d(ppc['branch'][:, 1], ppc_to)
ppc_idx = np.in1d(ppc['branch'][:, 0], ppc_from) \
& np.in1d(ppc['branch'][:, 1], ppc_to)
ppc["branch"][ppc_idx, BR_STATUS] = 0

# drop from in service lines as well
lines_is = lines_is.drop(sw_elem[~m])

# opened switches at in service lines
slidx = slidx\
& (np.in1d(net["switch"]["element"].values, lines_is.index)) \
& (np.in1d(net["switch"]["bus"].values, bus_is.index))
slidx = slidx \
& (np.in1d(net["switch"]["element"].values, lines_is.index)) \
& (np.in1d(net["switch"]["bus"].values, bus_is.index))
nlo = np.count_nonzero(slidx)

stidx = (net.switch["closed"].values == 0) & (net.switch["et"].values == "t")
Expand Down Expand Up @@ -587,9 +594,9 @@ def _switch_branches(net, ppc):
new_ls_buses[:] = np.array([0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1.1, 0.9])
new_ls_buses[:, 0] = new_indices
new_ls_buses[:, 9] = get_values(ppc["bus"][:, BASE_KV], ls_info[:, 1], bus_lookup)
# set voltage of new buses to voltage on other branch end
# set voltage of new buses to voltage on other branch end
to_buses = ppc["branch"][ls_info[ls_info[:, 0].astype(bool), 2], 1].real.astype(int)
from_buses = ppc["branch"][ls_info[np.logical_not(ls_info[:, 0]), 2], 0].real\
from_buses = ppc["branch"][ls_info[np.logical_not(ls_info[:, 0]), 2], 0].real \
.astype(int)
if len(to_buses):
ix = ls_info[:, 0] == 1
Expand Down Expand Up @@ -636,7 +643,7 @@ def _switch_branches(net, ppc):
ts_info[:, 1], bus_lookup)
# set voltage of new buses to voltage on other branch end
to_buses = ppc["branch"][ts_info[ts_info[:, 0].astype(bool), 2], 1].real.astype(int)
from_buses = ppc["branch"][ts_info[np.logical_not(ts_info[:, 0]), 2], 0].real\
from_buses = ppc["branch"][ts_info[np.logical_not(ts_info[:, 0]), 2], 0].real \
.astype(int)

# set newly created buses to voltage on other side of
Expand Down Expand Up @@ -769,12 +776,13 @@ def _update_trafo_trafo3w_ppc(net, ppc):

if trafo_end > line_end:
ppc["branch"][line_end:trafo_end,
[F_BUS, T_BUS, BR_R, BR_X, BR_B, TAP, SHIFT, BR_STATUS, RATE_A]] = \
[F_BUS, T_BUS, BR_R, BR_X, BR_B, TAP, SHIFT, BR_STATUS, RATE_A]] = \
_calc_trafo_parameter(net, ppc)
if trafo3w_end > trafo_end:
ppc["branch"][trafo_end:trafo3w_end, [F_BUS, T_BUS, BR_R, BR_X, BR_B, TAP, SHIFT, BR_STATUS]] = \
_calc_trafo3w_parameter(net, ppc)


def _calc_switch_parameter(net, ppc):
"""
calculates the line parameter in per unit.
Expand All @@ -801,15 +809,17 @@ def _calc_switch_parameter(net, ppc):
t[:, 2] = r_switch / baseR
return t


def _end_temperature_correction_factor(net):
if "endtemp_degree" not in net.line:
raise UserWarning("Specify end temperature for lines in net.endtemp_degree")
return (1 + .004 * (net.line.endtemp_degree.values.astype(float) - 20)) #formula from standard

return (1 + .004 * (net.line.endtemp_degree.values.astype(float) - 20)) # formula from standard


def _transformer_correction_factor(vsc, vscr, sn, cmax):
sn = sn / 1000.
zt = vsc / 100 / sn
rt = vscr / 100 / sn
xt = np.sqrt(zt**2 - rt**2)
xt = np.sqrt(zt ** 2 - rt ** 2)
kt = 0.95 * cmax / (1 + .6 * xt * sn)
return kt
return kt
3 changes: 1 addition & 2 deletions pandapower/converter/pypower/from_ppc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from pandas import Series, DataFrame, concat

import pandapower as pp
from pypower import ppoption
from pypower import runpf
from pypower import ppoption, runpf

try:
import pplog as logging
Expand Down
1 change: 0 additions & 1 deletion pandapower/converter/pypower/to_ppc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Energy System Technology (IWES), Kassel. All rights reserved. Use of this source code is governed
# by a BSD-style license that can be found in the LICENSE file.

from pandapower.powerflow import _pd2ppc
from pandapower.auxiliary import _add_ppc_options
from pandapower.powerflow import _pd2ppc

Expand Down
2 changes: 0 additions & 2 deletions pandapower/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from pandapower.run import runpp
from pandapower.diagnostic_reports import diagnostic_report
from pandapower.toolbox import get_connected_elements
from pandapower.powerflow import LoadflowNotConverged


# separator between log messages
log_message_sep = ("\n --------\n")
Expand Down
3 changes: 1 addition & 2 deletions pandapower/pypower_extensions/ext2int.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from copy import deepcopy
from warnings import warn

from numpy import array, zeros, argsort, arange
from numpy import flatnonzero as find
from numpy import array, zeros, argsort, arange, flatnonzero as find
from pypower.e2i_data import e2i_data
from pypower.e2i_field import e2i_field
from pypower.idx_area import PRICE_REF_BUS
Expand Down
3 changes: 1 addition & 2 deletions pandapower/pypower_extensions/makeBdc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Builds the B matrices and phase shift injections for DC power flow.
"""
from numpy import ones, r_, pi, flatnonzero as find
from numpy import real
from numpy import ones, r_, pi, flatnonzero as find, real
from pypower.idx_brch import F_BUS, T_BUS, BR_X, TAP, SHIFT, BR_STATUS
from pypower.idx_bus import BUS_I
from scipy.sparse import csr_matrix as sparse
Expand Down
26 changes: 5 additions & 21 deletions pandapower/pypower_extensions/makeYbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
from sys import stderr

from numba import jit
from numpy import ones, conj, nonzero, any, exp, pi, r_, argsort, resize, empty, complex128, zeros, int64, array, real
from pypower.idx_brch import F_BUS, T_BUS, BR_R, BR_X, BR_B, BR_STATUS, SHIFT, TAP
from numpy import any, r_, argsort, resize, empty, complex128, zeros, int64, array, real
from pypower.idx_brch import F_BUS, T_BUS
from pypower.idx_bus import BUS_I, GS, BS
from scipy.sparse import csr_matrix

from pandapower.pypower_extensions.makeYbus_pypower import branch_vectors


@jit(nopython=True, cache=True)
def gen_Ybus(Yf_x, Yt_x, Ysh, col_Y, f, t, f_sort, t_sort, nb, nl, r_nl):
"""
Expand Down Expand Up @@ -168,22 +171,3 @@ def makeYbus(baseMVA, bus, branch):


return Ybus, Yf, Yt

def branch_vectors(branch, nl):
stat = branch[:, BR_STATUS] ## ones at in-service branches
Ysf = stat / (branch[:, BR_R] + 1j * branch[:, BR_X]) ## series admittance
if any(branch[:, 17]) or any(branch[:, 18]):
Yst = stat / ((branch[:, BR_R] + branch[:, 17]) + 1j * (branch[:, BR_X] + branch[:, 18])) ## series admittance
else:
Yst = Ysf
Bc = stat * branch[:, BR_B] ## line charging susceptance
tap = ones(nl) ## default tap ratio = 1
i = nonzero(real(branch[:, TAP])) ## indices of non-zero tap ratios
tap[i] = real(branch[i, TAP]) ## assign non-zero tap ratios
tap = tap * exp(1j * pi / 180 * branch[:, SHIFT]) ## add phase shifters

Ytt = Yst + 1j * Bc / 2
Yff = (Ysf + 1j * Bc / 2) / (tap * conj(tap))
Yft = - Ysf / conj(tap)
Ytf = - Yst / tap
return Ytt, Yff, Yft, Ytf
Loading

0 comments on commit e1ef688

Please sign in to comment.