def test_change_type_line():
    net = pp.create_empty_network()
    r1 = 0.01
    x1 = 0.02
    c1 = 40
    i1 = 0.2
    name1 = "test_line1"
    typ1 = {"c_nf_per_km": c1, "r_ohm_per_km": r1, "x_ohm_per_km": x1, "max_i_ka": i1}
    pp.create_std_type(net, data=typ1, name=name1, element="line")
    
    r2 = 0.02
    x2 = 0.04
    c2 = 20
    i2 = 0.4
    name2 = "test_line2"
    typ2 = {"c_nf_per_km": c2, "r_ohm_per_km": r2, "x_ohm_per_km": x2, "max_i_ka": i2}
    pp.create_std_type(net, data=typ2, name=name2, element="line")

    b1 = pp.create_bus(net, vn_kv=0.4)
    b2 = pp.create_bus(net, vn_kv=0.4)
    lid = pp.create_line(net, b1, b2, 1., std_type=name1)
    assert net.line.r_ohm_per_km.at[lid] == r1
    assert net.line.x_ohm_per_km.at[lid] == x1
    assert net.line.c_nf_per_km.at[lid] == c1
    assert net.line.max_i_ka.at[lid] == i1
    assert net.line.std_type.at[lid] == name1
    
    pp.change_std_type(net, lid, name2)
    
    assert net.line.r_ohm_per_km.at[lid] == r2
    assert net.line.x_ohm_per_km.at[lid] == x2
    assert net.line.c_nf_per_km.at[lid] == c2
    assert net.line.max_i_ka.at[lid] == i2
    assert net.line.std_type.at[lid] == name2
def _change_to_ohl(net, idx_busbar, new_lines, n_cable):
    """
    This function changes line types from cable to ohl beginning at the end of the feeders in a \
    way, that the tapped line has the most portions of overhead-lines.
    """
    con_lines = pp.get_connected_elements(net, "line", idx_busbar) & new_lines
    cable_lines = con_lines
    last_con_lines = list(con_lines)

    while len(cable_lines) < n_cable:
        con_lines = sorted(pp.get_connected_elements(
                net, "line", net.line.to_bus.loc[last_con_lines]) & new_lines - cable_lines)
        last_con_lines = deepcopy(con_lines)
        while len(con_lines) > 0:
            cable_lines.add(con_lines.pop(0))
    for idx_line in list(new_lines - cable_lines):
        pp.change_std_type(net, idx_line, 'NFA2X 4x70', element="line")
def test_deviation_from_std_type(test_net, diag_params, diag_errors,
                                 report_methods):
    net = copy.deepcopy(test_net)
    check_function = 'deviation_from_std_type'
    diag_params = copy.deepcopy(diag_params)
    report_methods = copy.deepcopy(report_methods)
    net.line.r_ohm_per_km.loc[0] += 1
    net.line.x_ohm_per_km.loc[6] -= 1
    net.line.c_nf_per_km.loc[14] *= -1
    net.line.max_i_ka.loc[21] = '5'
    pp.change_std_type(net, 0, element='trafo', name='160 MVA 380/110 kV')
    net.trafo.vk_percent.loc[0] *= 2
    check_result = pp.deviation_from_std_type(net)
    if check_result:
        diag_results = {check_function: check_result}
    else:
        diag_results = {}
    assert diag_results[check_function] == \
    {'line': {0: {'e_value': 1.1571, 'param': 'r_ohm_per_km', 'std_type_in_lib': True,
                  'std_type_value': 0.1571},
              6: {'e_value': -0.883, 'param': 'x_ohm_per_km', 'std_type_in_lib': True,
                  'std_type_value': 0.117},
              14: {'e_value': -264.0, 'param': 'c_nf_per_km', 'std_type_in_lib': True,
                   'std_type_value': 264},
              21: {'e_value': '5', 'param': 'max_i_ka', 'std_type_in_lib': True,
                   'std_type_value': 0.105}},
    'trafo': {0: {'e_value': 24.4, 'param': 'vk_percent', 'std_type_in_lib': True,
                  'std_type_value': 12.2}}
    }

    for bool_value in [True, False]:
        diag_report = DiagnosticReports(net,
                                        diag_results,
                                        diag_errors,
                                        diag_params,
                                        compact_report=bool_value)
        report_check = None
        try:
            eval(report_methods[check_function])
            report_check = True
        except:
            report_check = False
        assert report_check
def test_repl_to_line():
    net = nw.simple_four_bus_system()
    idx = 0
    std_type = "NAYY 4x150 SE"
    new_idx = tb.repl_to_line(net, idx, std_type, in_service=True)
    pp.runpp(net)

    vm1 = net.res_bus.vm_pu.values
    va1 = net.res_bus.va_degree.values

    net.line.at[new_idx, "in_service"] = False
    pp.change_std_type(net, idx, std_type)
    pp.runpp(net)

    vm0 = net.res_bus.vm_pu.values
    va0 = net.res_bus.va_degree.values

    assert np.allclose(vm1, vm0)
    assert np.allclose(va1, va0)