Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
lthurner committed Mar 22, 2017
2 parents 7688357 + 74f3b58 commit 9f0701b
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 191 deletions.
126 changes: 66 additions & 60 deletions pandapower/auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
from pypower.idx_brch import F_BUS, T_BUS
from pypower.idx_bus import BUS_I, BUS_TYPE, NONE, PD, QD


try:
import pplog as logging
except:
import logging

logger = logging.getLogger(__name__)

class ADict(dict, MutableMapping):

class ADict(dict, MutableMapping):
def __init__(self, *args, **kwargs):
super(ADict, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -150,7 +149,6 @@ def _valid_name(cls, key):


class pandapowerNet(ADict):

def __init__(self, *args, **kwargs):
super(pandapowerNet, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -241,9 +239,9 @@ def get_values(source, selection, lookup):
:return:
"""
return np.array([source[lookup[k]] for k in selection])

def _select_is_elements(net):


def _select_is_elements(net):
"""
Selects certain "in_service" elements from net.
This is quite time consuming so it is done once at the beginning
Expand Down Expand Up @@ -281,7 +279,7 @@ def _select_is_elements(net):
, "shunt": np.in1d(net["shunt"].bus.values, bus_is_ind) \
& net["shunt"].in_service.values.astype(bool)
, "ext_grid": net["ext_grid"][np.in1d(net["ext_grid"].bus.values, bus_is_ind) \
& net["ext_grid"]["in_service"].values.astype(bool)]
& net["ext_grid"]["in_service"].values.astype(bool)]
, 'bus': net['bus'].iloc[:np.count_nonzero(bus_is)]
, 'line': net['line'].iloc[:np.count_nonzero(line_is)]
}
Expand All @@ -290,11 +288,11 @@ def _select_is_elements(net):
_is_elements = net['_is_elements']

bus_is_ind = _is_elements['bus'].index
#update elements
# update elements
elems = ['gen', 'ext_grid']
for elm in elems:
_is_elements[elm] = net[elm][np.in1d(net[elm].bus.values, bus_is_ind) \
& net[elm]["in_service"].values.astype(bool)]
& net[elm]["in_service"].values.astype(bool)]

else:
# select in service buses. needed for the other elements to be selected
Expand All @@ -303,55 +301,57 @@ def _select_is_elements(net):
bus_is_ind = net["bus"][bus_is].index
# check if in service elements are at in service buses
_is_elements = {
"gen" : net['gen'][np.in1d(net["gen"].bus.values, bus_is_ind) \
& net["gen"]["in_service"].values.astype(bool)]
, "load" : np.in1d(net["load"].bus.values, bus_is_ind) \
& net["load"].in_service.values.astype(bool)
, "sgen" : np.in1d(net["sgen"].bus.values, bus_is_ind) \
& net["sgen"].in_service.values.astype(bool)
, "ward" : np.in1d(net["ward"].bus.values, bus_is_ind) \
& net["ward"].in_service.values.astype(bool)
, "xward" : np.in1d(net["xward"].bus.values, bus_is_ind) \
& net["xward"].in_service.values.astype(bool)
, "shunt" : np.in1d(net["shunt"].bus.values, bus_is_ind) \
& net["shunt"].in_service.values.astype(bool)
, "ext_grid" : net["ext_grid"][np.in1d(net["ext_grid"].bus.values, bus_is_ind) \
& net["ext_grid"]["in_service"].values.astype(bool)]
"gen": net['gen'][np.in1d(net["gen"].bus.values, bus_is_ind) \
& net["gen"]["in_service"].values.astype(bool)]
, "load": np.in1d(net["load"].bus.values, bus_is_ind) \
& net["load"].in_service.values.astype(bool)
, "sgen": np.in1d(net["sgen"].bus.values, bus_is_ind) \
& net["sgen"].in_service.values.astype(bool)
, "ward": np.in1d(net["ward"].bus.values, bus_is_ind) \
& net["ward"].in_service.values.astype(bool)
, "xward": np.in1d(net["xward"].bus.values, bus_is_ind) \
& net["xward"].in_service.values.astype(bool)
, "shunt": np.in1d(net["shunt"].bus.values, bus_is_ind) \
& net["shunt"].in_service.values.astype(bool)
, "ext_grid": net["ext_grid"][np.in1d(net["ext_grid"].bus.values, bus_is_ind) \
& net["ext_grid"]["in_service"].values.astype(bool)]
, 'bus': net['bus'][bus_is]
, 'line': net['line'][line_is]
}

return _is_elements

def _add_ppc_options(net, calculate_voltage_angles, trafo_model, check_connectivity, mode,
copy_constraints_to_ppc, r_switch, init, enforce_q_lims, recycle):

def _add_ppc_options(net, calculate_voltage_angles, trafo_model, check_connectivity, mode,
copy_constraints_to_ppc, r_switch, init, enforce_q_lims, recycle):
"""
creates dictionary for pf, opf and short circuit calculations from input parameters.
"""
if recycle == None:
recycle = dict(_is_elements=False, ppc=False, Ybus=False)

options = {
"calculate_voltage_angles": calculate_voltage_angles
"calculate_voltage_angles": calculate_voltage_angles
, "trafo_model": trafo_model
, "check_connectivity": check_connectivity
, "mode": mode
, "copy_constraints_to_ppc": copy_constraints_to_ppc
, "r_switch": r_switch
, "init": init
, "init": init
, "enforce_q_lims": enforce_q_lims
, "recycle": recycle
}
}
_add_options(net, options)



def _add_pf_options(net, tolerance_kva, trafo_loading, numba, ac,
algorithm, max_iteration, **kwargs):
"""
creates dictionary for pf, opf and short circuit calculations from input parameters.
"""

options = {
"tolerance_kva": tolerance_kva
"tolerance_kva": tolerance_kva
, "trafo_loading": trafo_loading
, "numba": numba
, "ac": ac
Expand All @@ -361,7 +361,8 @@ def _add_pf_options(net, tolerance_kva, trafo_loading, numba, ac,

options.update(kwargs) # update options with some algorithm-specific parameters
_add_options(net, options)



def _add_opf_options(net, trafo_loading, ac, **kwargs):
"""
creates dictionary for pf, opf and short circuit calculations from input parameters.
Expand All @@ -374,35 +375,36 @@ def _add_opf_options(net, trafo_loading, ac, **kwargs):
options.update(kwargs) # update options with some algorithm-specific parameters
_add_options(net, options)


def _add_sc_options(net, fault, case, lv_tol_percent, tk_s, topology, r_fault_ohm,
x_fault_ohm, kappa, ip, ith, consider_sgens):
"""
creates dictionary for pf, opf and short circuit calculations from input parameters.
"""
options = {
"fault": fault
,"case": case
, "lv_tol_percent": lv_tol_percent
, "tk_s": tk_s
, "topology": topology
, "r_fault_ohm": r_fault_ohm
, "x_fault_ohm": x_fault_ohm
, "kappa": kappa
, "ip": ip
, "ith": ith
, "consider_sgens": consider_sgens
}
"fault": fault
, "case": case
, "lv_tol_percent": lv_tol_percent
, "tk_s": tk_s
, "topology": topology
, "r_fault_ohm": r_fault_ohm
, "x_fault_ohm": x_fault_ohm
, "kappa": kappa
, "ip": ip
, "ith": ith
, "consider_sgens": consider_sgens
}
_add_options(net, options)


def _add_options(net, options):
double_parameters = set(net._options.keys()) & set(options.keys())
if len(double_parameters) > 0:
raise UserWarning("Parameters always have to be unique! The following parameters where specified twice: %s"%double_parameters)
raise UserWarning(
"Parameters always have to be unique! The following parameters where specified twice: %s" % double_parameters)
net._options.update(options)


def _clean_up(net):
mode = net._options["mode"]
res_bus = net["res_bus_sc"] if mode == "sc" else net["res_bus"]
Expand All @@ -417,12 +419,13 @@ def _clean_up(net):
net["bus"].drop(xward_buses, inplace=True)
res_bus.drop(xward_buses, inplace=True)
net["xward"].drop(["ad_bus"], axis=1, inplace=True)

if len(net["dcline"]) > 0:
dc_gens = net.gen.index[(len(net.gen) - len(net.dcline)*2):]
dc_gens = net.gen.index[(len(net.gen) - len(net.dcline) * 2):]
net.gen.drop(dc_gens, inplace=True)
net.res_gen.drop(dc_gens, inplace=True)


def _set_isolated_buses_out_of_service(net, ppc):
# set disconnected buses out of service
# first check if buses are connected to branches
Expand Down Expand Up @@ -483,7 +486,7 @@ def calculate_line_results(net, use_res_bus_est=False):
res_line.i_to_ka = np.abs(line_currents_to) / 1e3
res_line.i_ka = np.fmax(res_line.i_from_ka, res_line.i_to_ka)
res_line.loading_percent = res_line.i_ka * 100. / net.line.max_i_ka.values \
/ net.line.df.values / net.line.parallel.values
/ net.line.df.values / net.line.parallel.values
res_line.p_from_kw = line_powers_from.real
res_line.q_from_kvar = line_powers_from.imag
res_line.p_to_kw = line_powers_to.real
Expand All @@ -492,6 +495,7 @@ def calculate_line_results(net, use_res_bus_est=False):
res_line.ql_kvar = res_line.q_from_kvar + res_line.q_to_kvar
return res_line


def _write_lookup_to_net(net, element, element_lookup):
"""
Updates selected lookups in net
Expand All @@ -511,7 +515,7 @@ def _check_connectivity(ppc):
bus_to = ppc['branch'][:, T_BUS].real.astype(int)

adj_matrix = sp.sparse.csr_matrix((np.ones(nobranch), (bus_from, bus_to)),
shape=(nobus, nobus))
shape=(nobus, nobus))

slacks = ppc['bus'][ppc['bus'][:, BUS_TYPE] == 3, BUS_I]

Expand All @@ -535,11 +539,12 @@ def _check_connectivity(ppc):
iso_p = abs(ppc['bus'][index_array, PD] * 1e3).sum()
iso_q = abs(ppc['bus'][index_array, QD] * 1e3).sum()
if iso_p > 0 or iso_q > 0:
logger.debug("%.0f kW active and %.0f kVar reactive power are unsupplied"%(iso_p, iso_q))
logger.debug("%.0f kW active and %.0f kVar reactive power are unsupplied" % (iso_p, iso_q))
else:
iso_p = iso_q = 0
return isolated_nodes, iso_p, iso_q



def _create_ppc2pd_bus_lookup(net):
# pd to ppc lookup
pd2ppc_bus_lookup = net["_pd2ppc_lookups"]["bus"]
Expand All @@ -554,11 +559,12 @@ def _create_ppc2pd_bus_lookup(net):
# store reverse lookup innet
net["_ppc2pd_lookups"]["bus"] = ppc2pd_bus_lookup


def _remove_isolated_elements_from_is_elements(net, isolated_nodes):
pcc2pd_bus_lookup = net["_ppc2pd_lookups"]["bus"]
ppc2pd_bus_lookup = net["_ppc2pd_lookups"]["bus"]
_is_elements = net["_is_elements"]
pp_nodes = [n for n in isolated_nodes if not(n > len(pcc2pd_bus_lookup))]
isolated_nodes_pp = pcc2pd_bus_lookup[pp_nodes]
pp_nodes = [n for n in isolated_nodes if not (n > len(ppc2pd_bus_lookup)-1)]
isolated_nodes_pp = ppc2pd_bus_lookup[pp_nodes]
# remove isolated buses from _is_elements["bus"]
_is_elements["bus"] = _is_elements["bus"].drop(set(isolated_nodes_pp) & set(_is_elements["bus"].index))
bus_is_ind = _is_elements["bus"].index
Expand All @@ -567,9 +573,9 @@ def _remove_isolated_elements_from_is_elements(net, isolated_nodes):
elems_to_update = ["load", "sgen", "ward", "xward", "shunt"]
for elem in elems_to_update:
_is_elements[elem] = np.in1d(net[elem].bus.values, bus_is_ind) \
& net[elem].in_service.values.astype(bool)
& net[elem].in_service.values.astype(bool)

_is_elements["gen"] = net['gen'][np.in1d(net["gen"].bus.values, bus_is_ind) \
& net["gen"]["in_service"].values.astype(bool)]
& net["gen"]["in_service"].values.astype(bool)]

net["_is_elements"] = _is_elements
net["_is_elements"] = _is_elements
21 changes: 11 additions & 10 deletions pandapower/build_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ def _switch_branches(net, ppc):
**ppc** - The PYPOWER format network to fill in values
"""
bus_lookup = net["_pd2ppc_lookups"]["bus"]
connectivity_check = net["_options"]["check_connectivity"]
mode = net._options["mode"]
# get in service elements
_is_elements = net["_is_elements"]
Expand All @@ -540,16 +541,16 @@ def _switch_branches(net, ppc):
to_bus = to_bus[~np.isnan(to_bus)].values.astype(int)

# set branch in ppc out of service if from and to bus are at a line which is in service
# if from_bus.size and to_bus.size:
# # 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["branch"][ppc_idx, BR_STATUS] = 0
#
# # drop from in service lines as well
# lines_is = lines_is.drop(sw_elem[~m])
if not connectivity_check and from_bus.size and to_bus.size:
# 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["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\
Expand Down
4 changes: 3 additions & 1 deletion pandapower/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,8 @@ def create_transformer_from_parameters(net, hv_bus, lv_bus, sn_kva, vn_hv_kv, vn
OPTIONAL:
**in_service** (boolean) - True for in_service or False for out of service
**parallel** (integer) - number of parallel transformers
**name** (string) - A custom name for this transformer
**shift_degree** (float) - Angle shift over the transformer*
Expand Down Expand Up @@ -2036,4 +2038,4 @@ def create_polynomial_cost(net, element, element_type, coefficients, type="p", i
create_line(net, 0, 1, length_km=1.23, std_type="NAYY 4x50 SE")
create_transformer(net, 0, 1, std_type="0.25 MVA 10/0.4 kV", tp_pos=3.2)
create_measurement(net, "v", "bus", 1.006, .004, bus=0, element=None)
create_measurement(net, "p", "line", 888, 8, bus=0, element=0)
create_measurement(net, "p", "line", 888, 8, bus=0, element=0)
16 changes: 8 additions & 8 deletions pandapower/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def diagnostic(net, report_style='detailed', warnings_only=False, return_result_
for diag_function in diag_functions:
if (diag_function in bus_index_dependent_checks) and ("missing_bus_indeces" in diag_results.keys()):
diag_result = "check skipped"
elif 'invalid_values' in diag_results.keys():
if (diag_function in invalid_value_dependent_checks) and (set(['gen', 'load', 'sgen'])
elif 'invalid_values' in diag_results.keys():
if (diag_function in invalid_value_dependent_checks) and (set(['gen', 'load', 'sgen'])
& set(diag_results['invalid_values'].keys())):
diag_result = "check skipped"
else:
Expand Down Expand Up @@ -638,7 +638,7 @@ def nominal_voltages_dont_match(net, nom_voltage_tolerance):
trafo3w_results['connectors_swapped_3w'] = connectors_swapped_3w
if trafo3w_results:
results['trafo3w'] = trafo3w_results

if len(results) > 0:
return results

Expand Down Expand Up @@ -832,8 +832,9 @@ def deviation_from_std_type(net):
check_results = {}
for key in net.std_types.keys():
for i, element in net[key].iterrows():
if element.std_type in net.std_types[key].keys():
std_type_values = net.std_types[key][element.std_type]
std_type = element.std_type
if std_type in net.std_types[key].keys():
std_type_values = net.std_types[key][std_type]
for param in std_type_values.keys():
if param == "tp_pos":
continue
Expand All @@ -844,7 +845,7 @@ def deviation_from_std_type(net):
check_results[key][i] = {'param': param, 'e_value': element[param],
'std_type_value': std_type_values[param],
'std_type_in_lib': True}
else:
elif std_type is not None:
if key not in check_results.keys():
check_results[key] = {}
check_results[key][i] = {'std_type_in_lib': False}
Expand All @@ -864,7 +865,7 @@ def parallel_switches(net):
OUTPUT:
**parallel_switches** (list) - List of tuples each containing parallel switches.
"""
Expand All @@ -874,6 +875,5 @@ def parallel_switches(net):
for bus, element in parallels_bus_and_element:
parallel_switches.append(list(net.switch[(net.switch.bus == bus)
& (net.switch.element == element)].index))

if parallel_switches:
return parallel_switches
Loading

0 comments on commit 9f0701b

Please sign in to comment.