Skip to content

Commit

Permalink
revise checks and create functions of cost tables; is related to e2nI…
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenMeinecke committed Nov 16, 2021
1 parent e22b91a commit 5fcd87e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 37 deletions.
23 changes: 17 additions & 6 deletions pandapower/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3521,7 +3521,7 @@ def create_pwl_cost(net, element, et, points, power_type="p", index=None, check=
create_pwl_cost(net, 0, "gen", [[0, 20, 1], [20, 30, 2]])
"""
element = element if not hasattr(element, "__iter__") else element[0]
if check and _cost_existance_check(net, element, et):
if check and _cost_existance_check(net, element, et, power_type=power_type):
raise UserWarning("There already exist costs for %s %i" % (et, element))

index = _get_index_with_check(net, "pwl_cost", index, "piecewise_linear_cost")
Expand Down Expand Up @@ -3599,11 +3599,22 @@ def _get_index_with_check(net, table, index, name=None):
return index


def _cost_existance_check(net, element, et):
return (bool(net.poly_cost.shape[0]) and
np_any((net.poly_cost.element == element).values & (net.poly_cost.et == et).values)) \
or (bool(net.pwl_cost.shape[0]) and
np_any((net.pwl_cost.element == element & net.pwl_cost.et == et).values))
def _cost_existance_check(net, element, et, power_type=None):
if power_type is None:
return (bool(net.poly_cost.shape[0]) and
np_any((net.poly_cost.element == element).values &
(net.poly_cost.et == et).values)) \
or (bool(net.pwl_cost.shape[0]) and
np_any((net.pwl_cost.element == element).values &
(net.pwl_cost.et == et).values))
else:
return (bool(net.poly_cost.shape[0]) and
np_any((net.poly_cost.element == element).values &
(net.poly_cost.et == et).values)) \
or (bool(net.pwl_cost.shape[0]) and
np_any((net.pwl_cost.element == element).values &
(net.pwl_cost.et == et).values &
(net.pwl_cost.power_type == power_type).values))


def _get_multiple_index_with_check(net, table, index, number, name=None):
Expand Down
8 changes: 6 additions & 2 deletions pandapower/opf/validate_opf_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def _check_necessary_opf_parameters(net, logger):
raise KeyError("OPF parameters are not set correctly. See error log.")

# --- log multiple costs to elements
if pd.concat([net.poly_cost[["element", "et"]], net.pwl_cost[["element", "et"]]]).duplicated(
).any():
cost_check_df = pd.concat([net.poly_cost[["element", "et"]], net.poly_cost[["element", "et"]]],
ignore_index=True)
cost_check_df["power_type"] = ["p"]*net.poly_cost.shape[0] + ["q"]*net.poly_cost.shape[0]
cost_check_df = pd.concat([cost_check_df, net.pwl_cost[["element", "et", "power_type"]]],
ignore_index=True)
if cost_check_df.duplicated().any():
raise UserWarning("There are multiple costs to one or multiple elements.")
53 changes: 29 additions & 24 deletions pandapower/test/opf/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def simple_opf_test_net():
net = pp.create_empty_network()
pp.create_bus(net, vn_kv=10.)
pp.create_bus(net, vn_kv=.4)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15, max_q_mvar=0.05,
min_q_mvar=-.005)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15,
max_q_mvar=0.05, min_q_mvar=-.005)
pp.create_ext_grid(net, 0)
pp.create_load(net, 1, p_mw=0.020, controllable=False)
pp.create_line_from_parameters(net, 0, 1, 50, name="line2", r_ohm_per_km=0.876,
Expand Down Expand Up @@ -138,8 +138,8 @@ def test_simplest_voltage():
# net = pp.create_empty_network()
# pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.)
# pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=.4)
# pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.150, max_q_mvar=0.05,
# min_q_mvar=-0.05)
# pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.150,
# max_q_mvar=0.05, min_q_mvar=-0.05)
# pp.create_ext_grid(net, 0, vm_pu=1.01)
# pp.create_load(net, 1, p_mw=0.02, controllable=False)
# pp.create_line_from_parameters(net, 0, 1, 50, name="line2", r_ohm_per_km=0.876,
Expand Down Expand Up @@ -170,8 +170,8 @@ def test_simplest_dispatch():
net = pp.create_empty_network()
pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.)
pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=.4)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.150, max_q_mvar=0.05,
min_q_mvar=-0.05)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.150,
max_q_mvar=0.05, min_q_mvar=-0.05)
pp.create_poly_cost(net, 0, "gen", cp1_eur_per_mw=100)
pp.create_ext_grid(net, 0)
pp.create_poly_cost(net, 0, "ext_grid", cp1_eur_per_mw=101)
Expand Down Expand Up @@ -323,7 +323,8 @@ def test_opf_gen_loading():
# run OPF

pp.runopp(net, OPF_VIOLATION=1e-1, OUT_LIM_LINE=2,
PDIPM_GRADTOL=1e-10, PDIPM_COMPTOL=1e-10, PDIPM_COSTTOL=1e-10, calculate_voltage_angles=False)
PDIPM_GRADTOL=1e-10, PDIPM_COMPTOL=1e-10, PDIPM_COSTTOL=1e-10,
calculate_voltage_angles=False)
assert net["OPF_converged"]

# assert and check result
Expand Down Expand Up @@ -401,8 +402,8 @@ def test_unconstrained_line():
net = pp.create_empty_network()
pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.)
pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=.4)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15, max_q_mvar=0.05,
min_q_mvar=-0.05)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15,
max_q_mvar=0.05, min_q_mvar=-0.05)
pp.create_ext_grid(net, 0)
pp.create_load(net, 1, p_mw=0.02, controllable=False)
pp.create_line_from_parameters(net, 0, 1, 50, name="line2", r_ohm_per_km=0.876,
Expand Down Expand Up @@ -430,8 +431,8 @@ def test_trafo3w_loading():
tidx = pp.create_transformer3w(net, b2, b3, b4, std_type='63/25/38 MVA 110/20/10 kV',
max_loading_percent=120)
pp.create_load(net, b3, p_mw=5, controllable=False)
load_id = pp.create_load(net, b4, p_mw=5, controllable=True, max_p_mw=50, min_p_mw=0, min_q_mvar=-1e6,
max_q_mvar=1e6)
load_id = pp.create_load(net, b4, p_mw=5, controllable=True, max_p_mw=50, min_p_mw=0,
min_q_mvar=-1e6, max_q_mvar=1e6)
pp.create_poly_cost(net, load_id, "load", cp1_eur_per_mw=-1000)
# pp.create_xward(net, b4, 1000, 1000, 1000, 1000, 0.1, 0.1, 1.0)
net.trafo3w.shift_lv_degree.at[tidx] = 120
Expand Down Expand Up @@ -530,10 +531,10 @@ def test_opf_varying_max_line_loading():
pfe_kw=0.11, name=None, in_service=True, index=None,
max_loading_percent=max_trafo_loading)

pp.create_sgen(net, 3, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15, max_q_mvar=0.025,
min_q_mvar=-0.025)
pp.create_sgen(net, 2, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15, max_q_mvar=0.025,
min_q_mvar=-0.025)
pp.create_sgen(net, 3, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15,
max_q_mvar=0.025, min_q_mvar=-0.025)
pp.create_sgen(net, 2, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15,
max_q_mvar=0.025, min_q_mvar=-0.025)
pp.create_poly_cost(net, 0, "sgen", cp1_eur_per_mw=10)
pp.create_poly_cost(net, 1, "sgen", cp1_eur_per_mw=10)
pp.create_ext_grid(net, 0)
Expand All @@ -549,7 +550,8 @@ def test_opf_varying_max_line_loading():
pp.runopp(net, init="flat", calculate_voltage_angles=False)
assert net["OPF_converged"]

assert np.allclose(net["_ppc"]["branch"][:, 5], np.array([0.02771281 + 0.j, 0.00692820 + 0.j, 0.12800000 + 0.j]))
assert np.allclose(net["_ppc"]["branch"][:, 5], np.array([0.02771281 + 0.j, 0.00692820 + 0.j,
0.12800000 + 0.j]))

# assert and check result
logger.debug("test_opf_sgen_loading")
Expand Down Expand Up @@ -719,7 +721,8 @@ def test_no_controllables(simple_opf_test_net):
try:
pp.runopp(net)
except pp.OPFNotConverged:
# opf will fail if not bus limits are set and vm_pu is the default value of 1.0 (it is enforced)
# opf will fail if not bus limits are set and vm_pu is the default value of 1.0 (
# it is enforced)
assert True
net.gen.loc[:, "vm_pu"] = 1.062 # vm_pu setpoint is mandatory if controllable=False
net.gen.loc[:, "p_mw"] = 0.149
Expand Down Expand Up @@ -807,8 +810,8 @@ def four_bus_net():


def test_three_slacks_vm_setpoint(four_bus_net):
# tests a net with three slacks in one area. Two of them will be converted to gens, since only one is allowed per
# area. The others should have vmin / vmax set as their vm_pu setpoint
# tests a net with three slacks in one area. Two of them will be converted to gens, since
# only one is allowed per area. The others should have vmin / vmax set as their vm_pu setpoint
net = four_bus_net
# create two additional slacks with different voltage setpoints
pp.create_ext_grid(net, 1, vm_pu=1.01, max_p_mw=1., min_p_mw=-1., min_q_mvar=-1, max_q_mvar=1.)
Expand All @@ -828,11 +831,13 @@ def test_only_gen_slack_vm_setpoint(four_bus_net):
net.bus.loc[:, "min_vm_pu"] = 0.9
net.bus.loc[:, "max_vm_pu"] = 1.1
# create two additional slacks with different voltage setpoints
pp.create_gen(net, 0, p_mw=0., vm_pu=1., max_p_mw=1., min_p_mw=-1., min_q_mvar=-1, max_q_mvar=1., slack=True)
g1 = pp.create_gen(net, 1, p_mw=0.02, vm_pu=1.01, max_p_mw=1., min_p_mw=-1., min_q_mvar=-1, max_q_mvar=1.,
controllable=False) # controllable == False -> vm_pu enforced
g3 = pp.create_gen(net, 3, p_mw=0.01, vm_pu=1.02, max_p_mw=1., min_p_mw=-1.,
min_q_mvar=-1, max_q_mvar=1.) # controllable == True -> vm_pu between bus voltages
pp.create_gen(net, 0, p_mw=0., vm_pu=1., max_p_mw=1., min_p_mw=-1., min_q_mvar=-1,
max_q_mvar=1., slack=True)
pp.create_gen(net, 1, p_mw=0.02, vm_pu=1.01, max_p_mw=1., min_p_mw=-1., min_q_mvar=-1,
max_q_mvar=1., controllable=False) # controllable == False -> vm_pu enforced
pp.create_gen(net, 3, p_mw=0.01, vm_pu=1.02, max_p_mw=1., min_p_mw=-1.,
min_q_mvar=-1, max_q_mvar=1.) # controllable == True -> vm_pu between
# bus voltages
pp.runpp(net)
# assert if voltage limits are correct in result in pf an opf
assert np.allclose(net.res_bus.loc[[0, 1, 3], "vm_pu"], [1., 1.01, 1.02])
Expand Down
8 changes: 4 additions & 4 deletions pandapower/test/opf/test_costs_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def test_mixed_p_q_pwl():
net = pp.create_empty_network()
pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.)
pp.create_bus(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=.4)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15, max_q_mvar=.05,
min_q_mvar=-.05)
pp.create_gen(net, 1, p_mw=0.1, controllable=True, min_p_mw=0.005, max_p_mw=0.15,
max_q_mvar=.05, min_q_mvar=-.05)
pp.create_ext_grid(net, 0)
pp.create_load(net, 1, p_mw=0.02, controllable=False, max_q_mvar=.05, max_p_mw=0.1, min_p_mw=0.005,
min_q_mvar=-.05)
pp.create_load(net, 1, p_mw=0.02, controllable=False, min_p_mw=0.005, max_p_mw=0.1,
max_q_mvar=.05, min_q_mvar=-.05)
pp.create_line_from_parameters(net, 0, 1, 50, name="line2", r_ohm_per_km=0.876,
c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,
max_loading_percent=100 * 690)
Expand Down
2 changes: 1 addition & 1 deletion pandapower/test/timeseries/test_output_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ def test_ow_index():
assert np.all(ow.output["res_line.loading_percent"].index == p_data.index)



def test_equal_eval_name_warning_and_costs():
net = nw.case5()
net.poly_cost = net.poly_cost.iloc[0:0]
pp.create_pwl_cost(net, 0, "sgen", [[0, 20, 1], [20, 30, 2]])
pp.create_pwl_cost(net, 0, "gen", [[0, 20, 1], [20, 30, 2]])
pp.create_pwl_cost(net, 1, "gen", [[0, 20, 1], [20, 30, 2]])
Expand Down

0 comments on commit 5fcd87e

Please sign in to comment.