# -*- coding: utf-8 -*-

# Copyright (c) 2016-2018 by University of Kassel and Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel. All rights reserved.


import numpy as np
import pytest
from pandapower.optimal_powerflow import OPFNotConverged


import pandapower as pp

try:
    import pplog as logging
except ImportError:
    import logging

logger = logging.getLogger(__name__)
logger.setLevel("DEBUG")


def test_cost_piecewise_linear_gen():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """
    # boundaries:
    vm_max = 1.05
    vm_min = 0.95

    # create net
    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_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=-150, max_q_kvar=50,
                  min_q_kvar=-50)
    pp.create_ext_grid(net, 0)
    pp.create_load(net, 1, p_kw=20, controllable=False)
    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)

    pp.create_piecewise_linear_cost(net, 0, "gen", np.array([[-150, -100], [-75, -50], [0, 0]]))
    # run OPF
    pp.runopp(net, verbose=False)

    assert net["OPF_converged"]
    assert net.res_cost - net.res_gen.p_kw.values / 1.5 < 1e-3


def test_cost_piecewise_linear_eg():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """

    # boundaries:
    vm_max = 1.05
    vm_min = 0.95

    # create net
    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=10)
    pp.create_ext_grid(net, 0, max_p_kw=0, min_p_kw=-50)
    pp.create_gen(net, 1, p_kw=-10, max_p_kw=0, min_p_kw=-50, controllable=True)
    # pp.create_ext_grid(net, 0)
    pp.create_load(net, 1, p_kw=20, controllable=False)
    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)

    pp.create_piecewise_linear_cost(net, 0, "ext_grid", np.array([[-50, -500], [0, 0]]))
    # run OPF
    pp.runopp(net, verbose=False)

    assert net["OPF_converged"]
    assert net.res_cost - - net.res_ext_grid.p_kw.values * 10 < 1e-3
    # check and assert result


def test_get_costs():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """

    # boundaries:
    vm_max = 1.05
    vm_min = 0.95

    # create net
    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_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=-150, max_q_kvar=50,
                  min_q_kvar=-50)
    pp.create_ext_grid(net, 0)
    pp.create_load(net, 1, p_kw=20, controllable=False)
    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)

    pp.create_piecewise_linear_cost(net, 0, "gen", np.array([[-150, -300], [0, 0]]))
    # run OPF
    pp.runopp(net, verbose=False)

    assert net["OPF_converged"]
    assert net.res_cost == 2 * net.res_gen.p_kw.values

    # check and assert result


def test_cost_piecewise_linear_sgen():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """

    # boundaries:
    vm_max = 1.05
    vm_min = 0.95

    # create net
    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_sgen(net, 1, p_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=-150, max_q_kvar=50,
                   min_q_kvar=-50)
    pp.create_ext_grid(net, 0)
    pp.create_load(net, 1, p_kw=20, controllable=False)
    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)

    pp.create_piecewise_linear_cost(net, 0, "sgen", np.array([[-150, -100], [-75, -50], [0, 0]]))
    # run OPF
    pp.runopp(net, verbose=False)

    assert net["OPF_converged"]
    assert net.res_cost - net.res_sgen.p_kw.values / 1.5 < 1e-3


def test_cost_piecewise_linear_load():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """

    # boundaries:
    vm_max = 1.05
    vm_min = 0.95

    # create net
    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_load(net, 1, p_kw=100, controllable=True, max_p_kw=150, min_p_kw=50, max_q_kvar=0,
                   min_q_kvar=0)
    pp.create_ext_grid(net, 0)
    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)

    pp.create_piecewise_linear_cost(net, 0, "load", np.array([[0, 0], [75, 50], [150, 100]]))
    # run OPF
    pp.runopp(net, verbose=False)

    assert net["OPF_converged"]
    assert abs(net.res_cost - net.res_load.p_kw.values / 1.5) < 1e-3

def test_cost_piecewise_linear_sgen_uneven_slopes():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """

    # boundaries:
    vm_max = 1.05
    vm_min = 0.95

    # create net
    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_sgen(net, 1, p_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=-150, max_q_kvar=50,
                   min_q_kvar=-50)
    pp.create_ext_grid(net, 0)
    pp.create_load(net, 1, p_kw=20, controllable=False)
    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)

    pp.create_piecewise_linear_cost(net, 0, "sgen", np.array([[-150, -200], [-75, -50], [0, 0]]))
    # run OPF
    pp.runopp(net, verbose=False)

    assert net["OPF_converged"]
    assert net.res_cost - net.res_sgen.p_kw.values / 1.5 < 1e-3


def test_cost_piecewise_linear_load_uneven_slopes():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """

    # boundaries:
    vm_max = 1.05
    vm_min = 0.95

    # create net
    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_load(net, 1, p_kw=100, controllable=True, max_p_kw=150, min_p_kw=50, max_q_kvar=0,
                   min_q_kvar=0)
    pp.create_ext_grid(net, 0)
    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)


    pp.create_piecewise_linear_cost(net, 0, "load", np.array([[0, 0], [75, 51], [150, 101]]))
    # run OPF
    with pytest.raises(OPFNotConverged):
        pp.runopp(net, verbose=False)
        assert net["OPF_converged"]
        assert abs(net.res_cost - net.res_load.p_kw.values / 1.5) < 1e-3

def test_cost_piecewise_linear_sgen_very_unsteady_slopes():
    """ Testing a very simple network for the resulting cost value
    constraints with OPF """

    # boundaries:
    vm_max = 1.5
    vm_min = 0.5

    # create net
    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_sgen(net, 1, p_kw=-1000, controllable=True, max_p_kw=0, min_p_kw=-1500,
                   max_q_kvar=50,
                   min_q_kvar=-50)
    pp.create_ext_grid(net, 0)
    pp.create_load(net, 1, p_kw=20, controllable=False)
    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)

    pp.create_piecewise_linear_cost(net, 0, "sgen", np.array([[-1500, 2],[-750,1 ], [0,2]]))
    # run OPF
    pp.runopp(net, verbose=False)

    assert net["OPF_converged"]
    # assert net.res_cost - net.res_sgen.p_kw.values / 1.5 < 1e-3




if __name__ == "__main__":
    # test_cost_piecewise_linear_sgen_very_unsteady_slopes()
    pytest.main(["test_costs_pwl.py", "-s"])

[((389, 416), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (406, 416), False, 'import logging\n'), ((660, 685), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (683, 685), True, 'import pandapower as pp\n'), ((690, 756), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (703, 756), True, 'import pandapower as pp\n'), ((760, 825), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(0.4)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=0.4)\n', (773, 825), True, 'import pandapower as pp\n'), ((829, 944), 'pandapower.create_gen', 'pp.create_gen', (['net', '(1)'], {'p_kw': '(-100)', 'controllable': '(True)', 'max_p_kw': '(-5)', 'min_p_kw': '(-150)', 'max_q_kvar': '(50)', 'min_q_kvar': '(-50)'}), '(net, 1, p_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=-\n    150, max_q_kvar=50, min_q_kvar=-50)\n', (842, 944), True, 'import pandapower as pp\n'), ((962, 988), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {}), '(net, 0)\n', (980, 988), True, 'import pandapower as pp\n'), ((993, 1044), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(20)', 'controllable': '(False)'}), '(net, 1, p_kw=20, controllable=False)\n', (1007, 1044), True, 'import pandapower as pp\n'), ((1049, 1227), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (1079, 1227), True, 'import pandapower as pp\n'), ((1405, 1434), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (1414, 1434), True, 'import pandapower as pp\n'), ((1749, 1774), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (1772, 1774), True, 'import pandapower as pp\n'), ((1779, 1845), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (1792, 1845), True, 'import pandapower as pp\n'), ((1849, 1913), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10)\n', (1862, 1913), True, 'import pandapower as pp\n'), ((1918, 1970), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {'max_p_kw': '(0)', 'min_p_kw': '(-50)'}), '(net, 0, max_p_kw=0, min_p_kw=-50)\n', (1936, 1970), True, 'import pandapower as pp\n'), ((1975, 2051), 'pandapower.create_gen', 'pp.create_gen', (['net', '(1)'], {'p_kw': '(-10)', 'max_p_kw': '(0)', 'min_p_kw': '(-50)', 'controllable': '(True)'}), '(net, 1, p_kw=-10, max_p_kw=0, min_p_kw=-50, controllable=True)\n', (1988, 2051), True, 'import pandapower as pp\n'), ((2089, 2140), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(20)', 'controllable': '(False)'}), '(net, 1, p_kw=20, controllable=False)\n', (2103, 2140), True, 'import pandapower as pp\n'), ((2145, 2323), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (2175, 2323), True, 'import pandapower as pp\n'), ((2493, 2522), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (2502, 2522), True, 'import pandapower as pp\n'), ((2858, 2883), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (2881, 2883), True, 'import pandapower as pp\n'), ((2888, 2954), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (2901, 2954), True, 'import pandapower as pp\n'), ((2958, 3023), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(0.4)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=0.4)\n', (2971, 3023), True, 'import pandapower as pp\n'), ((3027, 3142), 'pandapower.create_gen', 'pp.create_gen', (['net', '(1)'], {'p_kw': '(-100)', 'controllable': '(True)', 'max_p_kw': '(-5)', 'min_p_kw': '(-150)', 'max_q_kvar': '(50)', 'min_q_kvar': '(-50)'}), '(net, 1, p_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=-\n    150, max_q_kvar=50, min_q_kvar=-50)\n', (3040, 3142), True, 'import pandapower as pp\n'), ((3160, 3186), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {}), '(net, 0)\n', (3178, 3186), True, 'import pandapower as pp\n'), ((3191, 3242), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(20)', 'controllable': '(False)'}), '(net, 1, p_kw=20, controllable=False)\n', (3205, 3242), True, 'import pandapower as pp\n'), ((3247, 3425), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (3277, 3425), True, 'import pandapower as pp\n'), ((3591, 3620), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (3600, 3620), True, 'import pandapower as pp\n'), ((3960, 3985), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (3983, 3985), True, 'import pandapower as pp\n'), ((3990, 4056), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (4003, 4056), True, 'import pandapower as pp\n'), ((4060, 4125), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(0.4)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=0.4)\n', (4073, 4125), True, 'import pandapower as pp\n'), ((4129, 4245), 'pandapower.create_sgen', 'pp.create_sgen', (['net', '(1)'], {'p_kw': '(-100)', 'controllable': '(True)', 'max_p_kw': '(-5)', 'min_p_kw': '(-150)', 'max_q_kvar': '(50)', 'min_q_kvar': '(-50)'}), '(net, 1, p_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=\n    -150, max_q_kvar=50, min_q_kvar=-50)\n', (4143, 4245), True, 'import pandapower as pp\n'), ((4264, 4290), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {}), '(net, 0)\n', (4282, 4290), True, 'import pandapower as pp\n'), ((4295, 4346), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(20)', 'controllable': '(False)'}), '(net, 1, p_kw=20, controllable=False)\n', (4309, 4346), True, 'import pandapower as pp\n'), ((4351, 4529), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (4381, 4529), True, 'import pandapower as pp\n'), ((4708, 4737), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (4717, 4737), True, 'import pandapower as pp\n'), ((5055, 5080), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (5078, 5080), True, 'import pandapower as pp\n'), ((5085, 5151), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (5098, 5151), True, 'import pandapower as pp\n'), ((5155, 5220), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(0.4)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=0.4)\n', (5168, 5220), True, 'import pandapower as pp\n'), ((5224, 5335), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(100)', 'controllable': '(True)', 'max_p_kw': '(150)', 'min_p_kw': '(50)', 'max_q_kvar': '(0)', 'min_q_kvar': '(0)'}), '(net, 1, p_kw=100, controllable=True, max_p_kw=150, min_p_kw=\n    50, max_q_kvar=0, min_q_kvar=0)\n', (5238, 5335), True, 'import pandapower as pp\n'), ((5354, 5380), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {}), '(net, 0)\n', (5372, 5380), True, 'import pandapower as pp\n'), ((5385, 5563), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (5415, 5563), True, 'import pandapower as pp\n'), ((5738, 5767), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (5747, 5767), True, 'import pandapower as pp\n'), ((6103, 6128), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (6126, 6128), True, 'import pandapower as pp\n'), ((6133, 6199), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (6146, 6199), True, 'import pandapower as pp\n'), ((6203, 6268), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(0.4)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=0.4)\n', (6216, 6268), True, 'import pandapower as pp\n'), ((6272, 6388), 'pandapower.create_sgen', 'pp.create_sgen', (['net', '(1)'], {'p_kw': '(-100)', 'controllable': '(True)', 'max_p_kw': '(-5)', 'min_p_kw': '(-150)', 'max_q_kvar': '(50)', 'min_q_kvar': '(-50)'}), '(net, 1, p_kw=-100, controllable=True, max_p_kw=-5, min_p_kw=\n    -150, max_q_kvar=50, min_q_kvar=-50)\n', (6286, 6388), True, 'import pandapower as pp\n'), ((6407, 6433), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {}), '(net, 0)\n', (6425, 6433), True, 'import pandapower as pp\n'), ((6438, 6489), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(20)', 'controllable': '(False)'}), '(net, 1, p_kw=20, controllable=False)\n', (6452, 6489), True, 'import pandapower as pp\n'), ((6494, 6672), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (6524, 6672), True, 'import pandapower as pp\n'), ((6851, 6880), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (6860, 6880), True, 'import pandapower as pp\n'), ((7212, 7237), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (7235, 7237), True, 'import pandapower as pp\n'), ((7242, 7308), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (7255, 7308), True, 'import pandapower as pp\n'), ((7312, 7377), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(0.4)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=0.4)\n', (7325, 7377), True, 'import pandapower as pp\n'), ((7381, 7492), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(100)', 'controllable': '(True)', 'max_p_kw': '(150)', 'min_p_kw': '(50)', 'max_q_kvar': '(0)', 'min_q_kvar': '(0)'}), '(net, 1, p_kw=100, controllable=True, max_p_kw=150, min_p_kw=\n    50, max_q_kvar=0, min_q_kvar=0)\n', (7395, 7492), True, 'import pandapower as pp\n'), ((7511, 7537), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {}), '(net, 0)\n', (7529, 7537), True, 'import pandapower as pp\n'), ((7542, 7720), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (7572, 7720), True, 'import pandapower as pp\n'), ((8318, 8343), 'pandapower.create_empty_network', 'pp.create_empty_network', ([], {}), '()\n', (8341, 8343), True, 'import pandapower as pp\n'), ((8348, 8414), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(10.0)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=10.0)\n', (8361, 8414), True, 'import pandapower as pp\n'), ((8418, 8483), 'pandapower.create_bus', 'pp.create_bus', (['net'], {'max_vm_pu': 'vm_max', 'min_vm_pu': 'vm_min', 'vn_kv': '(0.4)'}), '(net, max_vm_pu=vm_max, min_vm_pu=vm_min, vn_kv=0.4)\n', (8431, 8483), True, 'import pandapower as pp\n'), ((8487, 8604), 'pandapower.create_sgen', 'pp.create_sgen', (['net', '(1)'], {'p_kw': '(-1000)', 'controllable': '(True)', 'max_p_kw': '(0)', 'min_p_kw': '(-1500)', 'max_q_kvar': '(50)', 'min_q_kvar': '(-50)'}), '(net, 1, p_kw=-1000, controllable=True, max_p_kw=0, min_p_kw=\n    -1500, max_q_kvar=50, min_q_kvar=-50)\n', (8501, 8604), True, 'import pandapower as pp\n'), ((8642, 8668), 'pandapower.create_ext_grid', 'pp.create_ext_grid', (['net', '(0)'], {}), '(net, 0)\n', (8660, 8668), True, 'import pandapower as pp\n'), ((8673, 8724), 'pandapower.create_load', 'pp.create_load', (['net', '(1)'], {'p_kw': '(20)', 'controllable': '(False)'}), '(net, 1, p_kw=20, controllable=False)\n', (8687, 8724), True, 'import pandapower as pp\n'), ((8729, 8907), 'pandapower.create_line_from_parameters', '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)'}), "(net, 0, 1, 50, name='line2', r_ohm_per_km=\n    0.876, c_nf_per_km=260.0, max_i_ka=0.123, x_ohm_per_km=0.1159876,\n    max_loading_percent=100 * 690)\n", (8759, 8907), True, 'import pandapower as pp\n'), ((9081, 9110), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (9090, 9110), True, 'import pandapower as pp\n'), ((9306, 9346), 'pytest.main', 'pytest.main', (["['test_costs_pwl.py', '-s']"], {}), "(['test_costs_pwl.py', '-s'])\n", (9317, 9346), False, 'import pytest\n'), ((1341, 1385), 'numpy.array', 'np.array', (['[[-150, -100], [-75, -50], [0, 0]]'], {}), '([[-150, -100], [-75, -50], [0, 0]])\n', (1349, 1385), True, 'import numpy as np\n'), ((2442, 2473), 'numpy.array', 'np.array', (['[[-50, -500], [0, 0]]'], {}), '([[-50, -500], [0, 0]])\n', (2450, 2473), True, 'import numpy as np\n'), ((3539, 3571), 'numpy.array', 'np.array', (['[[-150, -300], [0, 0]]'], {}), '([[-150, -300], [0, 0]])\n', (3547, 3571), True, 'import numpy as np\n'), ((4644, 4688), 'numpy.array', 'np.array', (['[[-150, -100], [-75, -50], [0, 0]]'], {}), '([[-150, -100], [-75, -50], [0, 0]])\n', (4652, 4688), True, 'import numpy as np\n'), ((5678, 5718), 'numpy.array', 'np.array', (['[[0, 0], [75, 50], [150, 100]]'], {}), '([[0, 0], [75, 50], [150, 100]])\n', (5686, 5718), True, 'import numpy as np\n'), ((6787, 6831), 'numpy.array', 'np.array', (['[[-150, -200], [-75, -50], [0, 0]]'], {}), '([[-150, -200], [-75, -50], [0, 0]])\n', (6795, 6831), True, 'import numpy as np\n'), ((7836, 7876), 'numpy.array', 'np.array', (['[[0, 0], [75, 51], [150, 101]]'], {}), '([[0, 0], [75, 51], [150, 101]])\n', (7844, 7876), True, 'import numpy as np\n'), ((7901, 7931), 'pytest.raises', 'pytest.raises', (['OPFNotConverged'], {}), '(OPFNotConverged)\n', (7914, 7931), False, 'import pytest\n'), ((7941, 7970), 'pandapower.runopp', 'pp.runopp', (['net'], {'verbose': '(False)'}), '(net, verbose=False)\n', (7950, 7970), True, 'import pandapower as pp\n'), ((9022, 9063), 'numpy.array', 'np.array', (['[[-1500, 2], [-750, 1], [0, 2]]'], {}), '([[-1500, 2], [-750, 1], [0, 2]])\n', (9030, 9063), True, 'import numpy as np\n')]