def test_3bus_with_transformer(): # 1. Create network net = pp.create_empty_network() pp.create_ext_grid(net, bus=3) pp.create_bus(net, name="bus1", vn_kv=10.) pp.create_bus(net, name="bus2", vn_kv=10.) pp.create_bus(net, name="bus3", vn_kv=10.) pp.create_bus(net, name="bus4", vn_kv=110.) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=.01, x_ohm_per_km=.03, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) pp.create_transformer(net, 3, 0, std_type="25 MVA 110/10 kV") pp.create_measurement(net, "v", "bus", 1.006, .004, bus=0) # V at bus 1 pp.create_measurement(net, "v", "bus", .968, .004, bus=1) # V at bus 2 pp.create_measurement(net, "p", "bus", -501, 10, 1) # P at bus 2 pp.create_measurement(net, "q", "bus", -286, 10, 1) # Q at bus 2 pp.create_measurement(net, "p", "line", 888, 8, 0, 0) # Pline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "p", "line", 1173, 8, 0, 1) # Pline (bus 1 -> bus 3) at bus 1 pp.create_measurement(net, "q", "line", 568, 8, 0, 0) # Qline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "q", "line", 663, 8, 0, 1) # Qline (bus 1 -> bus 3) at bus 1 pp.create_measurement(net, "p", "transformer", 2067, 10, bus=3, element=0) # transformer meas. pp.create_measurement(net, "q", "transformer", 1228, 10, bus=3, element=0) # at hv side # 2. Do state estimation success = estimate(net, init='flat') v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values target_v = np.array([ 0.98712592369954588, 0.98686806637143187, 0.98654891164725134, 0.98668652867504758 ]) diff_v = target_v - v_result target_delta = np.array( [0.56755308073946575, 0.55508269341754568, 0.54096568088774744, 0.0]) diff_delta = target_delta - delta_result assert success assert (np.nanmax(abs(diff_v)) < 1e-6) assert (np.nanmax(abs(diff_delta)) < 1e-6)
def add_virtual_pmu_meas_from_loadflow(net, v_std_dev=0.001, i_std_dev=0.1, p_std_dev=0.01, q_std_dev=0.01, dg_std_dev=0.1, seed=14, with_random_error=True): np.random.seed(seed) bus_meas_types = { 'v': 'vm_pu', "va": "va_degree", 'p': 'p_mw', 'q': 'q_mvar' } branch_meas_type = { 'line': { 'side': ('from', 'to'), 'meas_type': ('i_ka', 'ia_degree', 'p_mw', 'q_mvar') }, 'trafo': { 'side': ('hv', 'lv'), 'meas_type': ('i_ka', 'ia_degree', 'p_mw', 'q_mvar') }, 'trafo3w': { 'side': ('hv', 'mv', 'lv'), 'meas_type': ('i_ka', 'ia_degree', 'p_mw', 'q_mvar') } } # Added degree result for branches for br_type in branch_meas_type.keys(): for side in branch_meas_type[br_type]['side']: p, q, vm, va = net["res_" + br_type]["p_%s_mw" % side].values, \ net["res_" + br_type]["q_%s_mvar" % side].values, \ net["res_" + br_type]["vm_%s_pu" % side].values, \ net["res_" + br_type]["va_%s_degree" % side].values S = p + q * 1j V = vm * np.exp(np.deg2rad(va) * 1j) I = np.conj(S / V) net["res_" + br_type]["ia_%s_degree" % side] = np.rad2deg( np.angle(I)) for bus_ix, bus_res in net.res_bus.iterrows(): for meas_type in bus_meas_types.keys(): meas_value = float(bus_res[bus_meas_types[meas_type]]) if meas_type in ('p', 'q'): pp.create_measurement(net, meas_type=meas_type, element_type='bus', element=bus_ix, value=meas_value, std_dev=1) else: pp.create_measurement(net, meas_type=meas_type, element_type='bus', element=bus_ix, value=meas_value, std_dev=v_std_dev) for br_type in branch_meas_type.keys(): if not net['res_' + br_type].empty: for br_ix, br_res in net['res_' + br_type].iterrows(): for side in branch_meas_type[br_type]['side']: for meas_type in branch_meas_type[br_type]['meas_type']: pp.create_measurement( net, meas_type=meas_type.split("_")[0], element_type=br_type, element=br_ix, side=side, value=br_res[meas_type.split("_")[0] + '_' + side + '_' + meas_type.split("_")[1]], std_dev=1) if with_random_error: add_virtual_meas_error(net, v_std_dev=v_std_dev, p_std_dev=p_std_dev, q_std_dev=q_std_dev, i_std_dev=i_std_dev, dg_std_dev=dg_std_dev, with_random_error=with_random_error)
#create empty network net = pp.create_empty_network() #create buses b1 = pp.create_bus(net, name="bus 1", vn_kv=1., index=1) b2 = pp.create_bus(net, name="bus 2", vn_kv=1., index=2) b3 = pp.create_bus(net, name="bus 3", vn_kv=1., index=3) # set the slack bus to bus 1 pp.create_ext_grid(net, 1) #create lines l1 = pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=.01, x_ohm_per_km=.03, c_nf_per_km=0., max_i_ka=1) l2 = pp.create_line_from_parameters(net, 1, 3, 1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) l3 = pp.create_line_from_parameters(net, 2, 3, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) #create measurements pp.create_measurement(net, "v", "bus", 1.006, .004, element=b1) # V at bus 1 pp.create_measurement(net, "v", "bus", 0.968, .004, element=b2) # V at bus 2 pp.create_measurement(net, "p", "bus", -0.501, .0010, element=b2) # P at bus 2 pp.create_measurement(net, "q", "bus", -0.286, .0010, element=b2) # Q at bus 2 pp.create_measurement(net, "p", "line", 0.888, .008, element=l1, side=b1) # Pline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "p", "line", 1.173, .008, element=l2, side=b1) # Pline (bus 1 -> bus 3) at bus 1 pp.create_measurement(net, "q", "line", 0.568, .008, element=l1, side=b1) # Qline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "q", "line", 0.663, .008, element=l2, side=b1) # Qline (bus 1 -> bus 3) at bus 1 net.measurement success = estimate(net, init='flat') print(success) net.res_line_est
def test_3bus_with_bad_data(): net = pp.create_empty_network() pp.create_bus(net, name="bus1", vn_kv=1.) pp.create_bus(net, name="bus2", vn_kv=1.) pp.create_bus(net, name="bus3", vn_kv=1.) pp.create_ext_grid(net, 0) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=0.7, x_ohm_per_km=0.2, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=0.8, x_ohm_per_km=0.8, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=1, x_ohm_per_km=0.6, c_nf_per_km=0, max_i_ka=1) pp.create_measurement(net, "p", "line", -0.0011, 0.01, 0, 0) # p12 pp.create_measurement(net, "q", "line", 0.024, 0.01, 0, 0) # q12 pp.create_measurement(net, "p", "bus", -0.018, 0.01, 2) # p3 pp.create_measurement(net, "q", "bus", 0.1, 0.01, 2) # q3 pp.create_measurement(net, "v", "bus", 1.08, 0.05, 0) # u1 pp.create_measurement(net, "v", "bus", 1.015, 0.05, 2) # u3 # 0. Do chi2-test for corret data assert not chi2_analysis(net, init='flat') # 1. Create false voltage measurement for testing bad data detection (-> should be removed) pp.create_measurement(net, "v", "bus", 1.3, 0.01, 1) # V at bus 2 # 2. Do chi2-test bad_data_detected = chi2_analysis(net, init='flat') # 3. Perform rn_max_test success_rn_max = remove_bad_data(net, init='flat') v_est_rn_max = net.res_bus_est.vm_pu.values delta_est_rn_max = net.res_bus_est.va_degree.values target_v = np.array([1.0627, 1.0589, 1.0317]) diff_v = target_v - v_est_rn_max target_delta = np.array([0., 0.8677, 3.1381]) diff_delta = target_delta - delta_est_rn_max assert bad_data_detected assert success_rn_max if not (np.nanmax(abs(diff_v)) < 1e-4) or\ not (np.nanmax(abs(diff_delta)) < 1e-4): raise AssertionError("Estimation failed!")
successc = estimate(comm_feeder, init='flat') print(successc) #####MIXED ESTIMATOR############## successrc = estimate(mixed_feeder, init='flat') print(successrc) #################################################SET UP MEASUREMENTS FOR OVERALL FEEDER######################## j = 0 while j < 1: ##RESIDENTIAL i = 0 while i < len(res_feeder.res_bus_est): pp.create_measurement(net, 'v', 'bus', res_feeder.res_bus_est.vm_pu[i], 0.0004, element=i + 3) pp.create_measurement(net, 'p', 'bus', -res_feeder.res_bus_est.p_mw[i], 0.0010, element=i + 3) pp.create_measurement(net, 'q', 'bus', res_feeder.res_bus_est.p_mw[i], 0.0010, element=i + 3) i = i + 1
def create_net_with_bb_switch(): net = pp.create_empty_network() bus1 = pp.create_bus(net, name="bus1", vn_kv=10.) bus2 = pp.create_bus(net, name="bus2", vn_kv=10.) bus3 = pp.create_bus(net, name="bus3", vn_kv=10.) bus4 = pp.create_bus(net, name="bus4", vn_kv=10.) bus5 = pp.create_bus(net, name="bus5", vn_kv=110.) pp.create_line_from_parameters(net, bus1, bus2, 10, r_ohm_per_km=.59, x_ohm_per_km=.35, c_nf_per_km=10.1, max_i_ka=1) pp.create_transformer(net, bus5, bus1, std_type="40 MVA 110/10 kV") pp.create_ext_grid(net, bus=bus5, vm_pu=1.0) pp.create_load(net, bus1, p_mw=.350, q_mvar=.100) pp.create_load(net, bus2, p_mw=.450, q_mvar=.100) pp.create_load(net, bus3, p_mw=.250, q_mvar=.100) pp.create_load(net, bus4, p_mw=.150, q_mvar=.100) # Created bb switch pp.create_switch(net, bus2, element=bus3, et='b') pp.create_switch(net, bus1, element=bus4, et='b') pp.runpp(net, calculate_voltage_angles=True) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[bus1], .002), .002, element=bus1) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[bus3], .002), .002, element=bus3) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[bus5], .002), .002, element=bus5) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus5], .002), .002, element=bus5) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus5], .002), .002, element=bus5) # If measurement on the bus with bb-switch activated, it will incluence the results of the merged bus pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus4], .002), .002, element=bus4) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus4], .002), .002, element=bus4) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus3], .001), .001, element=bus3) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus3], .001), .001, element=bus3) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus2], .001), .001, element=bus2) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus2], .001), .001, element=bus2) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus1], .001), .001, element=bus1) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus1], .001), .001, element=bus1) pp.create_measurement(net, "p", "line", r2(net.res_line.p_from_mw.iloc[0], .002), .002, 0, side='from') pp.create_measurement(net, "q", "line", r2(net.res_line.q_from_mvar.iloc[0], .002), .002, 0, side='from') pp.create_measurement(net, "p", "trafo", r2(net.res_trafo.p_hv_mw.iloc[0], .001), .01, side="hv", element=0) pp.create_measurement(net, "q", "trafo", r2(net.res_trafo.q_hv_mvar.iloc[0], .001), .01, side="hv", element=0) return net
def test_zero_injection_aux_bus(): net = pp.create_empty_network() bus1 = pp.create_bus(net, name="bus1", vn_kv=10.) bus2 = pp.create_bus(net, name="bus2", vn_kv=10.) bus3 = pp.create_bus(net, name="bus3", vn_kv=10.) bus4 = pp.create_bus(net, name="bus4", vn_kv=110.) pp.create_line_from_parameters(net, bus1, bus2, 10, r_ohm_per_km=.59, x_ohm_per_km=.35, c_nf_per_km=10.1, max_i_ka=1) pp.create_line_from_parameters(net, bus2, bus3, 10, r_ohm_per_km=.59, x_ohm_per_km=.35, c_nf_per_km=10.1, max_i_ka=1) pp.create_transformer(net, bus4, bus1, std_type="40 MVA 110/10 kV") pp.create_ext_grid(net, bus=bus4, vm_pu=1.0) pp.create_load(net, bus1, p_mw=.350, q_mvar=.100) pp.create_load(net, bus2, p_mw=.450, q_mvar=.100) pp.create_load(net, bus3, p_mw=.250, q_mvar=.100) net.bus.at[bus3, 'in_service'] = False # Created bb switch pp.runpp(net, calculate_voltage_angles=True) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[bus1], .002), .002, element=bus1) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[bus4], .002), .002, element=bus4) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus4], .002), .002, element=bus4) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus4], .002), .002, element=bus4) # If measurement on the bus with bb-switch activated, it will incluence the results of the merged bus pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus2], .001), .001, element=bus2) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus2], .001), .001, element=bus2) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus1], .001), .001, element=bus1) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus1], .001), .001, element=bus1) pp.create_measurement(net, "p", "line", r2(net.res_line.p_from_mw.iloc[0], .002), .002, 0, side='from') pp.create_measurement(net, "q", "line", r2(net.res_line.q_from_mvar.iloc[0], .002), .002, 0, side='from') pp.create_measurement(net, "p", "trafo", r2(net.res_trafo.p_hv_mw.iloc[0], .001), .01, side="hv", element=0) pp.create_measurement(net, "q", "trafo", r2(net.res_trafo.q_hv_mvar.iloc[0], .001), .01, side="hv", element=0) net_auto = deepcopy(net) net_aux = deepcopy(net) success_none = estimate(net, tolerance=1e-5, zero_injection=None) # In this case zero_injection in mode "aux_bus" and "auto" should be exact the same success_aux = estimate(net_aux, tolerance=1e-5, zero_injection='aux_bus') success_auto = estimate(net_auto, tolerance=1e-5, zero_injection='auto') assert success_none and success_aux and success_auto assert np.allclose(net_auto.res_bus_est.va_degree.values,net_aux.res_bus_est.va_degree.values, 1e-4, equal_nan=True) assert np.allclose(net_auto.res_bus_est.vm_pu.values,net_aux.res_bus_est.vm_pu.values, 1e-4, equal_nan=True) # in case zero injection was set to none, the results should be different assert ~np.allclose(net.res_bus_est.vm_pu.values,net_aux.res_bus_est.vm_pu.values, 1e-2, equal_nan=True)
def test_3bus_with_pq_line_from_to_measurements(): np.random.seed(2017) net = load_3bus_network() net.measurement.drop(net.measurement.index, inplace=True) pp.create_load(net, 1, p_kw=495.974966, q_kvar=297.749528) pp.create_load(net, 2, p_kw=1514.220983, q_kvar=787.528929) pp.runpp(net) pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[0] * r(0.01), 0.01, 0) pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[2] * r(0.01), 0.01, 1) pp.create_measurement(net, "p", "bus", -net.res_bus.p_kw[0] * r(), max(1.0, abs(0.03 * net.res_bus.p_kw[0])), 0) pp.create_measurement(net, "q", "bus", -net.res_bus.q_kvar[0] * r(), max(1.0, abs(0.03 * net.res_bus.q_kvar[0])), 0) pp.create_measurement(net, "p", "bus", -net.res_bus.p_kw[2] * r(), max(1.0, abs(0.03 * net.res_bus.p_kw[2])), 2) pp.create_measurement(net, "q", "bus", -net.res_bus.q_kvar[2] * r(), max(1.0, abs(0.03 * net.res_bus.q_kvar[2])), 2) pp.create_measurement(net, "p", "line", net.res_line.p_from_kw[0] * r(), max(1.0, abs(0.03 * net.res_line.p_from_kw[0])), element=0, bus=0) pp.create_measurement(net, "q", "line", net.res_line.q_from_kvar[0] * r(), max(1.0, abs(0.03 * net.res_line.q_from_kvar[0])), element=0, bus=0) pp.create_measurement(net, "p", "line", net.res_line.p_to_kw[0] * r(), max(1.0, abs(0.03 * net.res_line.p_to_kw[0])), element=0, bus=1) pp.create_measurement(net, "q", "line", net.res_line.q_to_kvar[0] * r(), max(1.0, abs(0.03 * net.res_line.q_to_kvar[0])), element=0, bus=1) success = estimate(net, init='flat') assert success assert (np.nanmax( abs(net.res_bus_est.vm_pu.values - net.res_bus.vm_pu.values)) < 0.023) assert (np.nanmax( abs(net.res_bus_est.va_degree.values - net.res_bus.va_degree.values)) < 0.12)
def test_IEEE_case_9_with_bad_data(): # 1. Create network # test grid: IEEE case 9 (HV) # overall: 9 buses, 8 lines net = pp.networks.case9() pp.create_measurement(net, "v", "bus", 1.0, 0.01, bus=1) # V at bus 1 pp.create_measurement(net, "v", "bus", 1.02, 0.01, bus=2) # V at bus 2 pp.create_measurement(net, "v", "bus", 0.9822, 0.01, bus=3) # V at bus 3 pp.create_measurement(net, "v", "bus", 0.979, 0.01, bus=4) # V at bus 4 pp.create_measurement(net, "v", "bus", 1.005, 0.01, bus=5) # V at bus 5 pp.create_measurement(net, "v", "bus", 0.997, 0.01, bus=7) # V at bus 7 pp.create_measurement(net, "v", "bus", 0.953, 0.01, bus=8) # V at bus 8 pp.create_measurement(net, "p", "bus", 72000, 100., bus=0) pp.create_measurement(net, "p", "bus", 162780, 100., bus=1) pp.create_measurement(net, "p", "bus", 84958, 70., bus=2) pp.create_measurement(net, "p", "bus", 0., 1., bus=3) pp.create_measurement(net, "p", "bus", -89967., 20., bus=4) pp.create_measurement(net, "p", "bus", 0., 10., bus=5) pp.create_measurement(net, "p", "bus", -100059., 30., bus=6) pp.create_measurement(net, "p", "bus", 0., 10., bus=7) pp.create_measurement(net, "p", "bus", -125100, 50., bus=8) pp.create_measurement(net, "q", "bus", 24000, 100., bus=0) pp.create_measurement(net, "q", "bus", 14500, 100., bus=1) pp.create_measurement(net, "q", "bus", 3644, 70., bus=2) pp.create_measurement(net, "q", "bus", 0., 1., bus=3) pp.create_measurement(net, "q", "bus", -30041., 20., bus=4) pp.create_measurement(net, "q", "bus", 0., 10., bus=5) pp.create_measurement(net, "q", "bus", -35087, 30., bus=6) pp.create_measurement(net, "q", "bus", 0., 10., bus=7) pp.create_measurement(net, "q", "bus", -49900, 50., bus=8) # 2. Do state estimation success_SE = estimate(net, init='flat', ref_power=1e6) v_est_SE = net.res_bus_est.vm_pu.values delta_SE = net.res_bus_est.va_degree.values # 3. Create false measurement (very close to useful values) pp.create_measurement(net, "v", "bus", 0.2, 0.01, bus=0) # V at bus 0 # 4. Do chi2-test bad_data_detected = chi2_analysis(net, init='flat', ref_power=1e6) # 5. Perform rn_max_test success_rn_max = remove_bad_data(net, init='flat', ref_power=1e6, rn_max_threshold=7.0) v_est_rn_max = net.res_bus_est.vm_pu.values delta_est_rn_max = net.res_bus_est.va_degree.values diff_v = v_est_SE - v_est_rn_max diff_delta = delta_SE - delta_est_rn_max assert success_SE assert bad_data_detected assert success_rn_max assert (np.nanmax(abs(diff_v)) < 1e-5) assert (np.nanmax(abs(diff_delta)) < 1e-5)
def test_3bus_with_transformer(): np.random.seed(12) # 1. Create network net = pp.create_empty_network() pp.create_bus(net, name="bus1", vn_kv=10.) pp.create_bus(net, name="bus2", vn_kv=10.) pp.create_bus(net, name="bus3", vn_kv=10.) pp.create_bus(net, name="bus4", vn_kv=110.) pp.create_ext_grid(net, bus=3, vm_pu=1.01) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=.01, x_ohm_per_km=.03, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) pp.create_transformer(net, 3, 0, std_type="25 MVA 110/10 kV v1.4.3 and older") pp.create_load(net, 1, 450, 300) pp.create_load(net, 2, 350, 200) pp.runpp(net, calculate_voltage_angles=True) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[0], .004), .004, bus=0) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[1], .004), .004, bus=1) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[3], .004), .004, bus=3) pp.create_measurement(net, "p", "bus", -r2(net.res_bus.p_kw.iloc[1], 10), 10, bus=1) pp.create_measurement(net, "q", "bus", -r2(net.res_bus.q_kvar.iloc[1], 10), 10, bus=1) pp.create_measurement(net, "p", "bus", -r2(net.res_bus.p_kw.iloc[2], 10), 10, bus=2) pp.create_measurement(net, "q", "bus", -r2(net.res_bus.q_kvar.iloc[2], 10), 10, bus=2) pp.create_measurement(net, "p", "bus", 0., 1.0, bus=0) pp.create_measurement(net, "q", "bus", 0., 1.0, bus=0) pp.create_measurement(net, "p", "line", r2(net.res_line.p_from_kw.iloc[0], 8), 8, 0, 0) pp.create_measurement(net, "p", "line", r2(net.res_line.p_from_kw.iloc[1], 8), 8, 0, 1) pp.create_measurement(net, "p", "trafo", r2(net.res_trafo.p_hv_kw.iloc[0], 10), 10, bus=3, element=0) # transformer meas. pp.create_measurement(net, "q", "trafo", r2(net.res_trafo.q_hv_kvar.iloc[0], 10), 10, bus=3, element=0) # at hv side # 2. Do state estimation success = estimate(net, init='slack', tolerance=5e-5, maximum_iterations=10, calculate_voltage_angles=True) v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values diff_v = net.res_bus.vm_pu.values - v_result diff_delta = net.res_bus.va_degree.values - delta_result assert success assert (np.nanmax(abs(diff_v)) < 6e-4) assert (np.nanmax(abs(diff_delta)) < 1.4e-4) # Backwards check. Use state estimation results for power flow and check for equality net.load.drop(net.load.index, inplace=True) net.ext_grid.vm_pu = net.res_bus_est.vm_pu.iloc[net.ext_grid.bus.iloc[0]] pp.create_load(net, 0, net.res_bus_est.p_kw.iloc[0], net.res_bus_est.q_kvar.iloc[0]) pp.create_load(net, 1, net.res_bus_est.p_kw.iloc[1], net.res_bus_est.q_kvar.iloc[1]) pp.create_load(net, 2, net.res_bus_est.p_kw.iloc[2], net.res_bus_est.q_kvar.iloc[2]) _compare_pf_and_se_results(net)
def test_3bus_with_2_slacks(): # load the net which already contains 3 buses net = load_3bus_network() # add the same net with different slack (no galvanic connection) # skip bus index 4 as further stability test pp.create_bus(net, name="bus5", vn_kv=1., index=5) pp.create_bus(net, name="bus6", vn_kv=1., index=6) pp.create_bus(net, name="bus7", vn_kv=1., index=7) pp.create_ext_grid(net, 5) pp.create_line_from_parameters(net, 5, 6, 1, r_ohm_per_km=.01, x_ohm_per_km=.03, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 5, 7, 1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 6, 7, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) pp.create_measurement(net, "v", "bus", 1.006, .004, bus=5) # V at bus 5 pp.create_measurement(net, "v", "bus", .968, .004, bus=6) # V at bus 6 pp.create_measurement(net, "p", "bus", -501, 10, 6) # P at bus 6 pp.create_measurement(net, "q", "bus", -286, 10, 6) # Q at bus 6 pp.create_measurement(net, "p", "line", 888, 8, 5, 3) # Pline (bus 1 -> bus 2) at bus 5 pp.create_measurement(net, "p", "line", 1173, 8, 5, 4) # Pline (bus 1 -> bus 3) at bus 5 pp.create_measurement(net, "q", "line", 568, 8, 5, 3) # Qline (bus 1 -> bus 2) at bus 5 pp.create_measurement(net, "q", "line", 663, 8, 5, 4) # Qline (bus 1 -> bus 3) at bus 5 # 2. Do state estimation success = estimate(net, init='flat', maximum_iterations=10) v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values target_v = np.array( [0.9996, 0.9741, 0.9438, np.nan, 0.9996, 0.9741, 0.9438]) diff_v = target_v - v_result target_delta = np.array([ 0.0, -1.2475469989322963, -2.7457167371166862, np.nan, 0.0, -1.2475469989322963, -2.7457167371166862 ]) diff_delta = target_delta - delta_result assert success assert (np.nanmax(abs(diff_v)) < 1e-4) assert (np.nanmax(abs(diff_delta)) < 1e-4)
def test_init_slack_with_multiple_transformers(): np.random.seed(123) net = pp.create_empty_network() pp.create_bus(net, 220, index=0) pp.create_bus(net, 110, index=1) pp.create_bus(net, 110, index=2) pp.create_bus(net, 110, index=3) pp.create_bus(net, 10, index=4) pp.create_bus(net, 10, index=5) pp.create_bus(net, 10, index=6) pp.create_bus(net, 10, index=7, in_service=False) pp.create_transformer(net, 3, 7, std_type="63 MVA 110/10 kV", in_service=False) pp.create_transformer(net, 3, 4, std_type="63 MVA 110/10 kV") pp.create_transformer(net, 0, 1, std_type="100 MVA 220/110 kV") pp.create_line(net, 1, 2, 2.0, std_type="N2XS(FL)2Y 1x120 RM/35 64/110 kV") pp.create_line(net, 1, 3, 2.0, std_type="N2XS(FL)2Y 1x120 RM/35 64/110 kV") pp.create_line(net, 4, 5, 2.0, std_type="NA2XS2Y 1x95 RM/25 12/20 kV") pp.create_line(net, 5, 6, 2.0, std_type="NA2XS2Y 1x95 RM/25 12/20 kV") pp.create_load(net, 2, 5000, 3300) pp.create_load(net, 5, 900, 500) pp.create_load(net, 6, 700, 300) pp.create_ext_grid(net, bus=0, vm_pu=1.04, va_degree=10., name="Slack 220 kV") pp.runpp(net) for bus, row in net.res_bus[net.bus.in_service == True].iterrows(): pp.create_measurement(net, "v", "bus", row.vm_pu * r(0.01), 0.01, bus) if row.p_kw != 0.: continue pp.create_measurement(net, "p", "bus", -row.p_kw * r(), max(1.0, abs(0.03 * row.p_kw)), bus) pp.create_measurement(net, "q", "bus", -row.q_kvar * r(), max(1.0, abs(0.03 * row.q_kvar)), bus) pp.create_measurement(net, "p", "line", net.res_line.p_from_kw[0], 10., bus=1, element=0) pp.create_measurement(net, "q", "line", net.res_line.q_from_kvar[0], 10., bus=1, element=0) pp.create_measurement(net, "p", "line", net.res_line.p_from_kw[2], 10., bus=4, element=2) pp.create_measurement(net, "q", "line", net.res_line.q_from_kvar[2], 10., bus=4, element=2) pp.create_measurement(net, "p", "line", net.res_line.p_from_kw[3], 10., bus=5, element=3) pp.create_measurement(net, "q", "line", net.res_line.q_from_kvar[3], 10., bus=5, element=3) success = estimate(net, init='slack') diff_v = net.res_bus_est.vm_pu.values - np.asarray([ 1.044860374, 1.0425606695, 1.0423765983, 1.0425110929, 1.0412160717, 1.0294819221, 1.0244679562, np.nan ]) diff_delta = net.res_bus_est.va_degree.values - np.asarray([ 10., 9.5804972667, 9.5764432027, 9.5785762652, -140.5572134472, -140.5248734844, -140.5280590882, np.nan ]) assert success assert (np.nanmax(abs(diff_v)) < 1e-8) assert (np.nanmax(abs(diff_delta)) < 1e-8)
def test_IEEE_case_9_with_bad_data(): # 1. Create network # test grid: IEEE case 9 (HV) # overall: 9 buses, 8 lines # choosen standard deviations for different types of measurements: # - V: 0.01 p.u. # - P: 1000 kW (1kW for no load) # - Q: 1000 kVA (1kVA for no load) net = pp.networks.case9() pp.create_measurement(net, "v", "bus", 0.995, 0.01, bus=1) # V at bus 1 pp.create_measurement(net, "v", "bus", 0.992, 0.01, bus=2) # V at bus 2 pp.create_measurement(net, "v", "bus", 0.988, 0.01, bus=3) # V at bus 3 pp.create_measurement(net, "v", "bus", 0.969, 0.01, bus=4) # V at bus 4 pp.create_measurement(net, "v", "bus", 1.019, 0.01, bus=5) # V at bus 5 pp.create_measurement(net, "v", "bus", 0.999, 0.01, bus=7) # V at bus 7 pp.create_measurement(net, "v", "bus", 0.963, 0.01, bus=8) # V at bus 8 pp.create_measurement(net, "p", "bus", 73137., 1000., bus=0) # P at bus 0 pp.create_measurement(net, "p", "bus", 85133., 1000., bus=2) # P at bus 2 pp.create_measurement(net, "p", "bus", 0., 1., bus=3) # P at bus 3 pp.create_measurement(net, "p", "bus", 0., 1., bus=5) # P at bus 5 pp.create_measurement(net, "p", "bus", -99884., 1000., bus=6) # P at bus 6 pp.create_measurement(net, "p", "bus", 0., 10., bus=7) # P at bus 7 pp.create_measurement(net, "q", "bus", 24272., 1000., bus=0) # P at bus 0 pp.create_measurement(net, "q", "bus", 13969., 1000., bus=1) # P at bus 1 pp.create_measurement(net, "q", "bus", 4235., 1000., bus=2) # P at bus 2 pp.create_measurement(net, "q", "bus", 0., 1., bus=3) # Q at bus 3 pp.create_measurement(net, "q", "bus", -30177., 1000., bus=4) # Q at bus 4 pp.create_measurement(net, "q", "bus", 0., 10., bus=5) # Q at bus 5 pp.create_measurement(net, "q", "bus", -36856., 1000., bus=6) # Q at bus 6 pp.create_measurement(net, "q", "bus", 0., 1., bus=7) # Q at bus 7 pp.create_measurement(net, "q", "bus", -49673., 1000., bus=8) # Q at bus 8 # 2. Do state estimation success_SE = estimate(net, init='flat') v_est_SE = net.res_bus_est.vm_pu.values delta_SE = net.res_bus_est.va_degree.values # 3. Create false measurements pp.create_measurement(net, "p", "bus", 3000., 1000., bus=1) # P at bus 1 pp.create_measurement(net, "p", "bus", -2000., 1000., bus=4) # P at bus 4 # 4. Do chi2-test success_chi2 = chi2_analysis(net, init='flat') # 5. Perform rn_max_test success_rn_max = remove_bad_data(net, init='flat') v_est_rn_max = net.res_bus_est.vm_pu.values delta_est_rn_max = net.res_bus_est.va_degree.values diff_v = v_est_SE - v_est_rn_max diff_delta = delta_SE - delta_est_rn_max assert success_SE assert success_chi2 assert success_rn_max assert (np.nanmax(abs(diff_v)) < 1e-5) assert (np.nanmax(abs(diff_delta)) < 1e-5)
def test_3bus(): # 1. Create network net = pp.create_empty_network() pp.create_ext_grid(net, 0) pp.create_bus(net, name="bus1", vn_kv=1.) pp.create_bus(net, name="bus2", vn_kv=1.) pp.create_bus(net, name="bus3", vn_kv=1.) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=0.7, x_ohm_per_km=0.2, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=0.8, x_ohm_per_km=0.8, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=1, x_ohm_per_km=0.6, c_nf_per_km=0, max_i_ka=1) pp.create_measurement(net, "p", "line", -0.0011e3, 0.01e3, bus=0, element=0) # p12 pp.create_measurement(net, "q", "line", 0.024e3, 0.01e3, bus=0, element=0) # q12 pp.create_measurement(net, "p", "bus", 0.018e3, 0.01e3, bus=2) # p3 pp.create_measurement(net, "q", "bus", -0.1e3, 0.01e3, bus=2) # q3 pp.create_measurement(net, "v", "bus", 1.08, 0.05, 0) # u1 pp.create_measurement(net, "v", "bus", 1.015, 0.05, 2) # u3 # 2. Do state estimation success = estimate(net, init='flat') v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values target_v = np.array([1.0627, 1.0589, 1.0317]) diff_v = target_v - v_result target_delta = np.array([0., 0.8677, 3.1381]) diff_delta = target_delta - delta_result assert success assert (np.nanmax(abs(diff_v)) < 1e-4) assert (np.nanmax(abs(diff_delta)) < 1e-4)
def test_network_with_trafo3w_pq(): net = pp.create_empty_network() bus_slack = pp.create_bus(net, vn_kv=110) pp.create_ext_grid(net, bus=bus_slack) bus_20_1 = pp.create_bus(net, vn_kv=20,name="b") pp.create_sgen(net, bus=bus_20_1, p_mw=0.03, q_mvar=0.02) bus_10_1 = pp.create_bus(net, vn_kv=10) pp.create_sgen(net, bus=bus_10_1, p_mw=0.02, q_mvar=0.02) bus_10_2 = pp.create_bus(net, vn_kv=10) pp.create_load(net, bus=bus_10_2, p_mw=0.06, q_mvar=0.01) pp.create_line(net, from_bus=bus_10_1, to_bus=bus_10_2, std_type="149-AL1/24-ST1A 10.0", length_km=2) pp.create_transformer3w(net, bus_slack, bus_20_1, bus_10_1, std_type="63/25/38 MVA 110/20/10 kV") pp.runpp(net) pp.create_measurement(net, "p", "line", net.res_line.p_from_mw[0], 0.001, 0, 'from') pp.create_measurement(net, "q", "line", net.res_line.q_from_mvar[0], 0.001, 0, 'from') pp.create_measurement(net, "p", "line", net.res_line.p_to_mw[0], 0.001, 0, 'to') pp.create_measurement(net, "q", "line", net.res_line.q_to_mvar[0], 0.001, 0, 'to') pp.create_measurement(net, "p", "trafo3w", net.res_trafo3w.p_hv_mw[0], 0.001, 0, 'hv') pp.create_measurement(net, "q", "trafo3w", net.res_trafo3w.q_hv_mvar[0], 0.001, 0, 'hv') pp.create_measurement(net, "p", "trafo3w", net.res_trafo3w.p_mv_mw[0], 0.002, 0, 'mv') pp.create_measurement(net, "q", "trafo3w", net.res_trafo3w.q_mv_mvar[0], 0.002, 0, 'mv') pp.create_measurement(net, "p", "trafo3w", net.res_trafo3w.p_lv_mw[0], 0.001, 0, 'lv') pp.create_measurement(net, "q", "trafo3w", net.res_trafo3w.q_lv_mvar[0], 0.001, 0, 'lv') pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[0], 0.01, 0) pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[1], 0.01, 1) if not estimate(net): raise AssertionError("Estimation failed!") if not (np.nanmax(np.abs(net.res_bus.vm_pu.values - net.res_bus_est.vm_pu.values)) < 0.006) or\ not (np.nanmax(np.abs(net.res_bus.va_degree.values- net.res_bus_est.va_degree.values)) < 0.006): raise AssertionError("Estimation failed") #Try estimate with results initialization if not estimate(net, init="results"): raise AssertionError("Estimation failed!")
def test_init_slack_with_multiple_transformers(angles=True): np.random.seed(123) net = pp.create_empty_network() pp.create_bus(net, 220, index=0) pp.create_bus(net, 110, index=1) pp.create_bus(net, 110, index=2) pp.create_bus(net, 110, index=3) pp.create_bus(net, 10, index=4) pp.create_bus(net, 10, index=5) pp.create_bus(net, 10, index=6) pp.create_bus(net, 10, index=7, in_service=False) pp.create_transformer(net, 3, 7, std_type="63 MVA 110/10 kV v1.4.3 and older", in_service=False) pp.create_transformer(net, 3, 4, std_type="63 MVA 110/10 kV v1.4.3 and older") pp.create_transformer(net, 0, 1, std_type="100 MVA 220/110 kV") pp.create_line(net, 1, 2, 2.0, std_type="N2XS(FL)2Y 1x120 RM/35 64/110 kV") pp.create_line(net, 1, 3, 2.0, std_type="N2XS(FL)2Y 1x120 RM/35 64/110 kV") pp.create_line(net, 4, 5, 2.0, std_type="NA2XS2Y 1x95 RM/25 12/20 kV") pp.create_line(net, 5, 6, 2.0, std_type="NA2XS2Y 1x95 RM/25 12/20 kV") pp.create_load(net, 2, 5000, 3300) pp.create_load(net, 5, 900, 500) pp.create_load(net, 6, 700, 300) pp.create_ext_grid(net, bus=0, vm_pu=1.04, va_degree=10., name="Slack 220 kV") pp.runpp(net, calculate_voltage_angles=angles) for bus, row in net.res_bus[net.bus.in_service == True].iterrows(): pp.create_measurement(net, "v", "bus", row.vm_pu * r(0.01), 0.01, bus) if row.p_kw != 0.: continue pp.create_measurement(net, "p", "bus", -row.p_kw * r(), max(1.0, abs(0.03 * row.p_kw)), bus) pp.create_measurement(net, "q", "bus", -row.q_kvar * r(), max(1.0, abs(0.03 * row.q_kvar)), bus) pp.create_measurement(net, "p", "line", net.res_line.p_from_kw[0], 10., bus=1, element=0) pp.create_measurement(net, "q", "line", net.res_line.q_from_kvar[0], 10., bus=1, element=0) pp.create_measurement(net, "p", "line", net.res_line.p_from_kw[2], 10., bus=4, element=2) pp.create_measurement(net, "q", "line", net.res_line.q_from_kvar[2], 10., bus=4, element=2) pp.create_measurement(net, "p", "line", net.res_line.p_from_kw[3], 10., bus=5, element=3) pp.create_measurement(net, "q", "line", net.res_line.q_from_kvar[3], 10., bus=5, element=3) success = estimate(net, init='slack', calculate_voltage_angles=angles) # pretty high error for vm_pu (half percent!) assert success assert (np.nanmax( np.abs(net.res_bus.vm_pu.values - net.res_bus_est.vm_pu.values)) < 0.006) assert (np.nanmax( np.abs(net.res_bus.va_degree.values - net.res_bus_est.va_degree.values)) < 0.006)
def test_network_with_trafo3w_with_disabled_branch(): net = pp.create_empty_network() bus_slack = pp.create_bus(net, vn_kv=110) pp.create_ext_grid(net, bus=bus_slack) bus_20_1 = pp.create_bus(net, vn_kv=20,name="b") pp.create_sgen(net, bus=bus_20_1, p_mw=0.03, q_mvar=0.02) bus_10_1 = pp.create_bus(net, vn_kv=10) pp.create_sgen(net, bus=bus_10_1, p_mw=0.02, q_mvar=0.02) bus_10_2 = pp.create_bus(net, vn_kv=10) pp.create_load(net, bus=bus_10_2, p_mw=0.06, q_mvar=0.01) pp.create_line(net, from_bus=bus_10_1, to_bus=bus_10_2, std_type="149-AL1/24-ST1A 10.0", length_km=2) disabled_line = pp.create_line(net, from_bus=bus_10_1, to_bus=bus_10_2, std_type="149-AL1/24-ST1A 10.0", length_km=2) net.line.at[disabled_line, 'in_service'] = False pp.create_transformer3w(net, bus_slack, bus_20_1, bus_10_1, std_type="63/25/38 MVA 110/20/10 kV") pp.runpp(net) pp.create_measurement(net, "p", "line", net.res_line.p_from_mw[0], 0.001, 0, 'from') pp.create_measurement(net, "q", "line", net.res_line.q_from_mvar[0], 0.001, 0, 'from') pp.create_measurement(net, "p", "line", net.res_line.p_to_mw[0], 0.001, 0, 'to') pp.create_measurement(net, "q", "line", net.res_line.q_to_mvar[0], 0.001, 0, 'to') pp.create_measurement(net, "p", "line", net.res_line.p_to_mw[1], 0.001, 1, 'to') pp.create_measurement(net, "q", "line", net.res_line.q_to_mvar[1], 0.001, 1, 'to') pp.create_measurement(net, "p", "trafo3w", net.res_trafo3w.p_hv_mw[0], 0.001, 0, 'hv') pp.create_measurement(net, "q", "trafo3w", net.res_trafo3w.q_hv_mvar[0], 0.001, 0, 'hv') pp.create_measurement(net, "p", "trafo3w", net.res_trafo3w.p_mv_mw[0], 0.002, 0, 'mv') pp.create_measurement(net, "q", "trafo3w", net.res_trafo3w.q_mv_mvar[0], 0.002, 0, 'mv') pp.create_measurement(net, "p", "trafo3w", net.res_trafo3w.p_lv_mw[0], 0.001, 0, 'lv') pp.create_measurement(net, "q", "trafo3w", net.res_trafo3w.q_lv_mvar[0], 0.001, 0, 'lv') pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[0], 0.01, 0) pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[1], 0.01, 1) success = estimate(net) assert success assert (np.nanmax(np.abs(net.res_bus.vm_pu.values - net.res_bus_est.vm_pu.values)) < 0.006) assert (np.nanmax(np.abs(net.res_bus.va_degree.values- net.res_bus_est.va_degree.values)) < 0.006)
def test_3bus_with_out_of_service_bus(): # Test case from book "Power System State Estimation", A. Abur, A. G. Exposito, p. 20ff. # S_ref = 1 MVA (PP standard) # V_ref = 1 kV # Z_ref = 1 Ohm # The example only had per unit values, but pandapower expects kV, MVA, kW, kVar # Measurements should be in kW/kVar/A - Voltage in p.u. # 1. Create network net = pp.create_empty_network() pp.create_bus(net, name="bus1", vn_kv=1.) pp.create_bus(net, name="bus2", vn_kv=1.) pp.create_bus(net, name="bus3", vn_kv=1.) pp.create_bus(net, name="bus4", vn_kv=1., in_service=0) # out-of-service bus test pp.create_ext_grid(net, 0) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=.01, x_ohm_per_km=.03, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) pp.create_measurement(net, "v", "bus", 1.006, .004, 0) # V at bus 1 pp.create_measurement(net, "v", "bus", .968, .004, 1) # V at bus 2 pp.create_measurement(net, "p", "bus", .501, .010, 1) # P at bus 2 pp.create_measurement(net, "q", "bus", .286, .010, 1) # Q at bus 2 pp.create_measurement(net, "p", "line", .888, .008, 0, 0) # Pline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "p", "line", 1.173, .008, 1, 0) # Pline (bus 1 -> bus 3) at bus 1 pp.create_measurement(net, "q", "line", .568, .008, 0, 0) # Qline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "q", "line", .663, .008, 1, 0) # Qline (bus 1 -> bus 3) at bus 1 # 2. Do state estimation if not estimate(net, init='flat'): raise AssertionError("Estimation failed!") v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values target_v = np.array([[0.9996, 0.9741, 0.9438, np.nan]]) diff_v = target_v - v_result target_delta = np.array([[0., -1.2475, -2.7457, np.nan]]) diff_delta = target_delta - delta_result if not (np.nanmax(abs(diff_v)) < 1e-4) or\ not (np.nanmax(abs(diff_delta)) < 1e-4): raise AssertionError("Estimation failed!")
def test_net_with_zero_injection(): # @author: AndersLi net = pp.create_empty_network() b1 = pp.create_bus(net, name="Bus 1", vn_kv=220, index=1) b2 = pp.create_bus(net, name="Bus 2", vn_kv=220, index=2) b3 = pp.create_bus(net, name="Bus 3", vn_kv=220, index=3) b4 = pp.create_bus(net, name="Bus 4", vn_kv=220, index=4) pp.create_ext_grid(net, b1) # set the slack bus to bus 1 factor = 48.4 * 2 * np.pi * 50 * 1e-9 # capacity factor pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=.0221*48.4, x_ohm_per_km=.1603*48.4, c_nf_per_km=0.00274/factor, max_i_ka=1) pp.create_line_from_parameters(net, 2, 3, 1, r_ohm_per_km=.0428*48.4, x_ohm_per_km=.242*48.4, c_nf_per_km=0.00384/factor, max_i_ka=1) l3 = pp.create_line_from_parameters(net, 2, 4, 1, r_ohm_per_km=.002*48.4, x_ohm_per_km=.0111*48.4, c_nf_per_km=0.00018/factor, max_i_ka=1) pp.create_measurement(net, "v", "bus", 1.063548, .001, b1) # V at bus 1 pp.create_measurement(net, "v", "bus", 1.068342, .001, b3) # V at bus 3 pp.create_measurement(net, "v", "bus", 1.069861, .001, b4) # V at bus 4 pp.create_measurement(net, "p", "bus", 40.0, 1, b1) # P at bus 1 pp.create_measurement(net, "q", "bus", 9.2, 1, b1) # Q at bus 1 # pp.create_measurement(net, "p", "bus", 0, 0.01, b2) # P at bus 2 - not required anymore # pp.create_measurement(net, "q", "bus", 0, 0.01, b2) # Q at bus 2 - not required anymore pp.create_measurement(net, "p", "bus", -10.0, 1, b3) # P at bus 3 pp.create_measurement(net, "q", "bus", -1.0, 1, b3) # Q at bus 3 pp.create_measurement(net, "p", "bus", -30.0, 1, b4) # P at bus 4 pp.create_measurement(net, "q", "bus", 0.100, 1, b4) # Q at bus 4 pp.create_measurement(net, "p", "line", 30.100, 1, l3, side="to") # Pline (bus 2 -> bus 4) at bus 4 pp.create_measurement(net, "q", "line", -0.099, 1, l3, side="to") # Qline (bus 2 -> bus 4) at bus 4 estimate(net, tolerance=1e-10, zero_injection='auto', algorithm='wls_with_zero_constraint') assert np.abs(net.res_bus_est.at[b2, 'p_mw']) < 1e-8 assert np.abs(net.res_bus_est.at[b2, 'q_mvar']) < 1e-8 net_given_bus = deepcopy(net) success = estimate(net, tolerance=1e-6, zero_injection="auto") success_given_bus = estimate(net, tolerance=1e-6, zero_injection=[b2]) assert success and success_given_bus assert np.allclose(net.res_bus_est.va_degree.values,net_given_bus.res_bus_est.va_degree.values, 1e-3) assert np.allclose(net.res_bus_est.vm_pu.values,net_given_bus.res_bus_est.vm_pu.values, 1e-3)
def test_3bus_with_transformer(): np.random.seed(12) # 1. Create network net = pp.create_empty_network() pp.create_bus(net, name="bus1", vn_kv=10.) pp.create_bus(net, name="bus2", vn_kv=10.) pp.create_bus(net, name="bus3", vn_kv=10.) pp.create_bus(net, name="bus4", vn_kv=110.) pp.create_ext_grid(net, bus=3, vm_pu=1.01) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=.01, x_ohm_per_km=.03, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) pp.create_std_type(net, {"sn_mva": 25, "vn_hv_kv": 110, "vn_lv_kv": 10, "vk_percent": 10.04, "vkr_percent": 0.276, "pfe_kw": 28.51, "i0_percent": 0.073, "shift_degree": 150, "tap_side": "hv", "tap_neutral": 0, "tap_min": -9, "tap_max": 9, "tap_step_degree": 0, "tap_step_percent": 1.5, "tap_phase_shifter": False}, "25 MVA 110/10 kV v1.4.3 and older", element="trafo") pp.create_transformer(net, 3, 0, std_type="25 MVA 110/10 kV v1.4.3 and older") pp.create_load(net, bus=1, p_mw=0.45, q_mvar=0.3) pp.create_load(net, bus=2, p_mw=0.35, q_mvar=0.2) pp.runpp(net, calculate_voltage_angles=True) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[0], .004), .004, element=0) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[1], .004), .004, element=1) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[3], .004), .004, element=3) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[1], .01), .01, element=1) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[1], .01), .01, element=1) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[2], .01), .010, element=2) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[2], .01), .01, element=2) pp.create_measurement(net, "p", "bus", 0., 0.001, element=0) pp.create_measurement(net, "q", "bus", 0., 0.001, element=0) pp.create_measurement(net, "p", "line", r2(net.res_line.p_from_mw.iloc[0], .008), .008, 0, 0) pp.create_measurement(net, "p", "line", r2(net.res_line.p_from_mw.iloc[1], .008), .008, 1, 0) pp.create_measurement(net, "p", "trafo", r2(net.res_trafo.p_hv_mw.iloc[0], .01), .01, side="hv", element=0) # transformer meas. pp.create_measurement(net, "q", "trafo", r2(net.res_trafo.q_hv_mvar.iloc[0], .01), .01, side=3, element=0) # at hv side # 2. Do state estimation if not estimate(net, init='slack', tolerance=1e-6, maximum_iterations=10, calculate_voltage_angles=True): raise AssertionError("Estimation failed!") v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values diff_v = net.res_bus.vm_pu.values - v_result diff_delta = net.res_bus.va_degree.values - delta_result if not (np.nanmax(abs(diff_v)) < 6e-4) or\ not (np.nanmax(abs(diff_delta)) < 8e-4): raise AssertionError("Estimation failed!") # Backwards check. Use state estimation results for power flow and check for equality net.load.drop(net.load.index, inplace=True) net.ext_grid.vm_pu = net.res_bus_est.vm_pu.iloc[net.ext_grid.bus.iloc[0]] pp.create_load(net, 0, net.res_bus_est.p_mw.iloc[0], net.res_bus_est.q_mvar.iloc[0]) pp.create_load(net, 1, net.res_bus_est.p_mw.iloc[1], net.res_bus_est.q_mvar.iloc[1]) pp.create_load(net, 2, net.res_bus_est.p_mw.iloc[2], net.res_bus_est.q_mvar.iloc[2]) _compare_pf_and_se_results(net)
def test_net_unobserved_island(): net = pp.create_empty_network() bus1 = pp.create_bus(net, name="bus1", vn_kv=10.) bus2 = pp.create_bus(net, name="bus2", vn_kv=10.) bus3 = pp.create_bus(net, name="bus3", vn_kv=10.) bus4 = pp.create_bus(net, name="bus4", vn_kv=110.) pp.create_line_from_parameters(net, bus1, bus2, 10, r_ohm_per_km=.59, x_ohm_per_km=.35, c_nf_per_km=10.1, max_i_ka=1) pp.create_line_from_parameters(net, bus2, bus3, 10, r_ohm_per_km=.59, x_ohm_per_km=.35, c_nf_per_km=10.1, max_i_ka=1) pp.create_transformer(net, bus4, bus1, std_type="40 MVA 110/10 kV") pp.create_ext_grid(net, bus=bus4, vm_pu=1.0) pp.create_load(net, bus1, p_mw=.350, q_mvar=.100) pp.create_load(net, bus2, p_mw=.450, q_mvar=.100) pp.create_load(net, bus3, p_mw=.250, q_mvar=.100) # Created bb switch pp.runpp(net, calculate_voltage_angles=True) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[bus1], .002), .002, element=bus1) pp.create_measurement(net, "v", "bus", r2(net.res_bus.vm_pu.iloc[bus4], .002), .002, element=bus4) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus4], .002), .002, element=bus4) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus4], .002), .002, element=bus4) # IF pq of bus2 is not available makes bus3 an unobserved island # pp.create_measurement(net, "p", "bus", -r2(net.res_bus.p_mw.iloc[bus2], .001), .001, element=bus2) # pp.create_measurement(net, "q", "bus", -r2(net.res_bus.q_mvar.iloc[bus2], .001), .001, element=bus2) pp.create_measurement(net, "p", "bus", r2(net.res_bus.p_mw.iloc[bus1], .001), .001, element=bus1) pp.create_measurement(net, "q", "bus", r2(net.res_bus.q_mvar.iloc[bus1], .001), .001, element=bus1) pp.create_measurement(net, "p", "line", r2(net.res_line.p_from_mw.iloc[0], .002), .002, 0, side='from') pp.create_measurement(net, "q", "line", r2(net.res_line.q_from_mvar.iloc[0], .002), .002, 0, side='from') pp.create_measurement(net, "p", "trafo", r2(net.res_trafo.p_hv_mw.iloc[0], .001), .01, side="hv", element=0) pp.create_measurement(net, "q", "trafo", r2(net.res_trafo.q_hv_mvar.iloc[0], .001), .01, side="hv", element=0) if not estimate(net, tolerance=1e-6, zero_injection=None): raise AssertionError("Estimation failed!")
def test_3bus_with_2_slacks(): # load the net which already contains 3 buses net = load_3bus_network() # add the same net with different slack (no galvanic connection) # skip bus index 4 as further stability test pp.create_bus(net, name="bus5", vn_kv=1., index=5) pp.create_bus(net, name="bus6", vn_kv=1., index=6) pp.create_bus(net, name="bus7", vn_kv=1., index=7) pp.create_ext_grid(net, 5) pp.create_line_from_parameters(net, 5, 6, 1, r_ohm_per_km=.01, x_ohm_per_km=.03, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 5, 7, 1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) pp.create_line_from_parameters(net, 6, 7, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) pp.create_measurement(net, "v", "bus", 1.006, .004, element=5) # V at bus 5 pp.create_measurement(net, "v", "bus", .968, .004, element=6) # V at bus 6 pp.create_measurement(net, "p", "bus", .501, .010, element=6) # P at bus 6 pp.create_measurement(net, "q", "bus", .286, .010, element=6) # Q at bus 6 pp.create_measurement(net, "p", "line", .888, .008, 3, 5) # Pline (bus 5 -> bus 6) at bus 5 pp.create_measurement(net, "p", "line", 1.173, .008, 4, 5) # Pline (bus 5 -> bus 7) at bus 5 pp.create_measurement(net, "q", "line", .568, .008, 3, 5) # Qline (bus 5 -> bus 6) at bus 5 pp.create_measurement(net, "q", "line", .663, .008, 4, 5) # Qline (bus 5 -> bus 7) at bus 5 # 2. Do state estimation if not estimate(net, init='flat', maximum_iterations=10): raise AssertionError("Estimation failed!") v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values target_v = np.array([0.9996, 0.9741, 0.9438, np.nan, 0.9996, 0.9741, 0.9438]) target_delta = np.array([0.0, -1.2475469989322963, -2.7457167371166862, np.nan, 0.0, -1.2475469989322963, -2.7457167371166862]) if not np.allclose(v_result, target_v, atol=1e-4, equal_nan=True) or\ not np.allclose(delta_result, target_delta, atol=1e-4, equal_nan=True): raise AssertionError("Estimation failed!")
def test_example_simple(): net = example_simple() # --- fix scaling net.load["scaling"] = 1. # --- add some additional data net.bus["subnet"] = ["net%i" % i for i in net.bus.index] pp.create_measurement(net, "i", "trafo", np.nan, np.nan, 0, "hv", name="1") pp.create_measurement(net, "i", "line", np.nan, np.nan, 1, "to", name="2") pp.create_measurement(net, "v", "bus", np.nan, np.nan, 0, name="3") net.shunt["max_step"] = np.nan stor = pp.create_storage(net, 6, 0.01, 0.1, -0.002, 0.05, 80, name="sda", min_p_mw=-0.01, max_p_mw=0.008, min_q_mvar=-0.01, max_q_mvar=0.005) net.storage.loc[stor, "efficiency_percent"] = 90 net.storage.loc[stor, "self-discharge_percent_per_day"] = 0.3 pp.create_dcline(net, 4, 6, 0.01, 0.1, 1e-3, 1.0, 1.01, name="df", min_q_from_mvar=-0.01) pp.runpp(net) to_drop = pp.create_bus(net, 7, "to_drop") # --- add names to elements for i in pp.pp_elements(): net[i] = ensure_full_column_data_existence(net, i, 'name') avoid_duplicates_in_column(net, i, 'name') # --- create geodata net.bus_geodata["x"] = [0, 1, 2, 3, 4, 5, 5, 3.63] net.bus_geodata["y"] = [0]*5+[-5, 5, 2.33] merge_busbar_coordinates(net) # --- convert csv_data = pp2csv_data(net, export_pp_std_types=True, drop_inactive_elements=True) net_from_csv_data = csv_data2pp(csv_data) # --- adjust net appearance pp.drop_buses(net, [to_drop]) del net["OPF_converged"] net.load["type"] = np.nan del net_from_csv_data["substation"] del net_from_csv_data["profiles"] for key in net.keys(): if isinstance(net[key], pd.DataFrame): # drop unequal columns dummy_columns = net[key].columns extra_columns = net_from_csv_data[key].columns.difference(dummy_columns) net_from_csv_data[key].drop(columns=extra_columns, inplace=True) # drop result table rows if "res_" in key: if not key == "res_bus": net[key].drop(net[key].index, inplace=True) else: net[key].loc[:, ["p_mw", "q_mvar"]] = np.nan # adjust dtypes if net[key].shape[0]: try: net_from_csv_data[key] = net_from_csv_data[key].astype(dtype=dict(net[ key].dtypes)) except: logger.error("dtype adjustment of %s failed." % key) eq = pp.nets_equal(net, net_from_csv_data, tol=1e-7) assert eq
def test_3bus_with_side_names(): np.random.seed(2017) net = load_3bus_network() net.measurement.drop(net.measurement.index, inplace=True) pp.create_load(net, 1, p_mw=0.495974966, q_mvar=0.297749528) pp.create_load(net, 2, p_mw=1.514220983, q_mvar=0.787528929) pp.runpp(net) pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[0] * r(0.01), 0.01, 0) pp.create_measurement(net, "v", "bus", net.res_bus.vm_pu[2] * r(0.01), 0.01, 1) pp.create_measurement(net, "p", "bus", net.res_bus.p_mw[0] * r(), max(1.0e-3, abs(0.03 * net.res_bus.p_mw[0])), 0) pp.create_measurement(net, "q", "bus", net.res_bus.q_mvar[0] * r(), max(1.0e-3, abs(0.03 * net.res_bus.q_mvar[0])), 0) pp.create_measurement(net, "p", "bus", net.res_bus.p_mw[2] * r(), max(1.0e-3, abs(0.03 * net.res_bus.p_mw[2])), 2) pp.create_measurement(net, "q", "bus", net.res_bus.q_mvar[2] * r(), max(1.0e-3, abs(0.03 * net.res_bus.q_mvar[2])), 2) pp.create_measurement(net, "p", "line", net.res_line.p_from_mw[0] * r(), max(1.0e-3, abs(0.03 * net.res_line.p_from_mw[0])), element=0, side="from") pp.create_measurement(net, "q", "line", net.res_line.q_from_mvar[0] * r(), max(1.0e-3, abs(0.03 * net.res_line.q_from_mvar[0])), element=0, side="from") pp.create_measurement(net, "p", "line", net.res_line.p_to_mw[0] * r(), max(1.0e-3, abs(0.03 * net.res_line.p_to_mw[0])), element=0, side="to") pp.create_measurement(net, "q", "line", net.res_line.q_to_mvar[0] * r(), max(1.0e-3, abs(0.03 * net.res_line.q_to_mvar[0])), element=0, side="to") if not estimate(net, init='flat'): raise AssertionError("Estimation failed!") assert (np.nanmax(abs(net.res_bus_est.vm_pu.values - net.res_bus.vm_pu.values)) < 0.023) assert (np.nanmax(abs(net.res_bus_est.va_degree.values - net.res_bus.va_degree.values)) < 0.12)
def add_virtual_meas_from_loadflow(net, v_std_dev=0.01, p_std_dev=0.03, q_std_dev=0.03, seed=14, with_random_error=False): np.random.seed(seed) bus_meas_types = {'v': 'vm_pu', 'p': 'p_mw', 'q': 'q_mvar'} branch_meas_type = { 'line': { 'side': ('from', 'to'), 'meas_type': ('p_mw', 'q_mvar') }, 'trafo': { 'side': ('hv', 'lv'), 'meas_type': ('p_mw', 'q_mvar') }, 'trafo3w': { 'side': ('hv', 'mv', 'lv'), 'meas_type': ('p_mw', 'q_mvar') } } for bus_ix, bus_res in net.res_bus.iterrows(): for meas_type in bus_meas_types.keys(): meas_value = float(bus_res[bus_meas_types[meas_type]]) if meas_type in ('p', 'q'): pp.create_measurement(net, meas_type=meas_type, element_type='bus', element=bus_ix, value=meas_value, std_dev=1) else: pp.create_measurement(net, meas_type=meas_type, element_type='bus', element=bus_ix, value=meas_value, std_dev=v_std_dev) for br_type in branch_meas_type.keys(): if not net['res_' + br_type].empty: for br_ix, br_res in net['res_' + br_type].iterrows(): for side in branch_meas_type[br_type]['side']: for meas_type in branch_meas_type[br_type]['meas_type']: pp.create_measurement( net, meas_type=meas_type[0], element_type=br_type, element=br_ix, side=side, value=br_res[meas_type[0] + '_' + side + meas_type[1:]], std_dev=1) add_virtual_meas_error(net, v_std_dev=v_std_dev, p_std_dev=p_std_dev, q_std_dev=q_std_dev, with_random_error=with_random_error)
def test_init_slack_with_multiple_transformers(angles=True): np.random.seed(123) net = pp.create_empty_network() pp.create_bus(net, 220, index=0) pp.create_bus(net, 110, index=1) pp.create_bus(net, 110, index=2) pp.create_bus(net, 110, index=3) pp.create_bus(net, 10, index=4) pp.create_bus(net, 10, index=5) pp.create_bus(net, 10, index=6) pp.create_bus(net, 10, index=7, in_service=False) pp.create_std_type(net, {"sn_mva": 63, "vn_hv_kv": 110, "vn_lv_kv": 10, "vk_percent": 10.04, "vkr_percent": 0.31, "pfe_kw": 31.51, "i0_percent": 0.078, "shift_degree": 150, "tap_side": "hv", "tap_neutral": 0, "tap_min": -9, "tap_max": 9, "tap_step_degree": 0, "tap_step_percent": 1.5, "tap_phase_shifter": False}, "63 MVA 110/10 kV v1.4.3 and older", element="trafo") pp.create_transformer(net, 3, 7, std_type="63 MVA 110/10 kV v1.4.3 and older", in_service=False) pp.create_transformer(net, 3, 4, std_type="63 MVA 110/10 kV v1.4.3 and older") pp.create_transformer(net, 0, 1, std_type="100 MVA 220/110 kV") pp.create_line(net, 1, 2, 2.0, std_type="N2XS(FL)2Y 1x120 RM/35 64/110 kV") pp.create_line(net, 1, 3, 2.0, std_type="N2XS(FL)2Y 1x120 RM/35 64/110 kV") pp.create_line(net, 4, 5, 2.0, std_type="NA2XS2Y 1x95 RM/25 12/20 kV") pp.create_line(net, 5, 6, 2.0, std_type="NA2XS2Y 1x95 RM/25 12/20 kV") pp.create_load(net, 2, p_mw=5, q_mvar=3.3) pp.create_load(net, 5, p_mw=0.9, q_mvar=0.5) pp.create_load(net, bus=6, p_mw=0.7, q_mvar=0.3) pp.create_ext_grid(net, bus=0, vm_pu=1.04, va_degree=10., name="Slack 220 kV") pp.runpp(net, calculate_voltage_angles=angles) for bus, row in net.res_bus[net.bus.in_service == True].iterrows(): pp.create_measurement(net, "v", "bus", row.vm_pu * r(0.01), 0.01, bus) if row.p_mw != 0.: continue pp.create_measurement(net, "p", "bus", row.p_mw * r(), max(0.001, abs(0.03 * row.p_mw)), bus) pp.create_measurement(net, "q", "bus", row.q_mvar * r(), max(.0001, abs(0.03 * row.q_mvar)), bus) pp.create_measurement(net, "p", "line", net.res_line.p_from_mw[0], .01, side=1, element=0) pp.create_measurement(net, "q", "line", net.res_line.q_from_mvar[0], 0.01, side=1, element=0) pp.create_measurement(net, "p", "line", net.res_line.p_from_mw[2], .01, side=4, element=2) pp.create_measurement(net, "q", "line", net.res_line.q_from_mvar[2], .01, side=4, element=2) pp.create_measurement(net, "p", "line", net.res_line.p_from_mw[3], .01, side=5, element=3) pp.create_measurement(net, "q", "line", net.res_line.q_from_mvar[3], 0.01, side=5, element=3) success = estimate(net, init='slack', calculate_voltage_angles=angles, tolerance=1e-9) # pretty high error for vm_pu (half percent!) assert success assert (np.nanmax(np.abs(net.res_bus.vm_pu.values - net.res_bus_est.vm_pu.values)) < 0.006) assert (np.nanmax(np.abs(net.res_bus.va_degree.values- net.res_bus_est.va_degree.values)) < 0.006)
1, r_ohm_per_km=.02, x_ohm_per_km=.05, c_nf_per_km=0., max_i_ka=1) l3 = pp.create_line_from_parameters(net, 2, 3, 1, r_ohm_per_km=.03, x_ohm_per_km=.08, c_nf_per_km=0., max_i_ka=1) # bus voltages pp.create_measurement(net, "v", "bus", 1.006, 0.004, b1) pp.create_measurement(net, "v", "bus", .968, 0.004, b2) # bus, p, q pp.create_measurement(net, "p", "bus", -0.501, 10., b2) pp.create_measurement(net, "q", "bus", -.266, 10., b2) # line pp.create_measurement(net, "p", "line", 0.888, 8., l1, side=1) pp.create_measurement(net, "p", "line", 1.173, 8., l2, side=1) pp.create_measurement(net, "q", "line", 0.568, 8., l1, side=1) pp.create_measurement(net, "q", "line", 0.663, 8., l2, side=1) est.estimate(net) print(net.res_bus_est)
def test_3bus(): # 1. Create network net = pp.create_empty_network() pp.create_bus(net, name="bus1", vn_kv=1.) pp.create_bus(net, name="bus2", vn_kv=1.) pp.create_bus(net, name="bus3", vn_kv=1.) pp.create_ext_grid(net, 0) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=0.7, x_ohm_per_km=0.2, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=0.8, x_ohm_per_km=0.8, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=1, x_ohm_per_km=0.6, c_nf_per_km=0, max_i_ka=1) pp.create_measurement(net, "p", "line", -0.0011, 0.01, 0, 0) # p12 pp.create_measurement(net, "q", "line", 0.024, 0.01, 0, 0) # q12 pp.create_measurement(net, "p", "bus", -0.018, 0.01, 2) # p3 pp.create_measurement(net, "q", "bus", 0.1, 0.01, 2) # q3 pp.create_measurement(net, "v", "bus", 1.08, 0.05, 0) # u1 pp.create_measurement(net, "v", "bus", 1.015, 0.05, 2) # u3 # 2. Do state estimation if not estimate(net, init='flat'): raise AssertionError("Estimation failed!") v_result = net.res_bus_est.vm_pu.values delta_result = net.res_bus_est.va_degree.values target_v = np.array([1.0627, 1.0589, 1.0317]) diff_v = target_v - v_result target_delta = np.array([0., 0.8677, 3.1381]) diff_delta = target_delta - delta_result if not (np.nanmax(abs(diff_v)) < 1e-4) or\ not (np.nanmax(abs(diff_delta)) < 1e-4): raise AssertionError("Estimation failed!") # Backwards check. Use state estimation results for power flow and check for equality net.ext_grid.vm_pu = net.res_bus_est.vm_pu.iloc[0] pp.create_load(net, 0, net.res_bus_est.p_mw.iloc[0], net.res_bus_est.q_mvar.iloc[0]) pp.create_load(net, 1, net.res_bus_est.p_mw.iloc[1], net.res_bus_est.q_mvar.iloc[1]) pp.create_load(net, 2, net.res_bus_est.p_mw.iloc[2], net.res_bus_est.q_mvar.iloc[2]) _compare_pf_and_se_results(net)
def create_measurement_unit(df_measurement, net): list_value = [] list_std = [] for index, row in df_measurement.iterrows(): if row['element_type'] == 'bus': if row['meas_type'] == 'v': mu = net.res_bus.iloc[row['element'], 0] sigma = (abs(mu) * upper_bus_accuracy - abs(mu) * lower_bus_accuracy) / 4 elif row['meas_type'] == 'p': mu = net.res_bus.iloc[row['element'], 2] sigma = (abs(mu) * upper_bus_accuracy - abs(mu) * lower_bus_accuracy) / 4 elif row['meas_type'] == 'q': mu = net.res_bus.iloc[row['element'], 3] sigma = (abs(mu) * upper_bus_accuracy - abs(mu) * lower_bus_accuracy) / 4 elif row['element_type'] == 'line': if row['side'] == 'from': if row['meas_type'] == 'p': mu = net.res_line.iloc[row['element'], 0] sigma = (abs(mu) * upper_line_accuracy - abs(mu) * lower_line_accuracy) / 4 elif row['meas_type'] == 'q': mu = net.res_line.iloc[row['element'], 1] sigma = (abs(mu) * upper_line_accuracy - abs(mu) * lower_line_accuracy) / 4 elif row['meas_type'] == 'i': mu = net.res_line.iloc[row['element'], 6] sigma = (abs(mu) * upper_line_accuracy - abs(mu) * lower_line_accuracy) / 4 elif row['side'] == 'to': if row['meas_type'] == 'p': mu = net.res_line.iloc[row['element'], 2] sigma = (abs(mu) * upper_line_accuracy - abs(mu) * lower_line_accuracy) / 4 elif row['meas_type'] == 'q': mu = net.res_line.iloc[row['element'], 3] sigma = (abs(mu) * upper_line_accuracy - abs(mu) * lower_line_accuracy) / 4 elif row['meas_type'] == 'i': mu = net.res_line.iloc[row['element'], 7] sigma = (abs(mu) * upper_line_accuracy - abs(mu) * lower_line_accuracy) / 4 elif row['element_type'] == 'trafo': if row['side'] == 'from': if row['meas_type'] == 'i': mu = net.res_trafo.iloc[row['element'], 6] sigma = (abs(mu) * upper_trafo_accuracy - abs(mu) * lower_trafo_accuracy) / 4 elif row['meas_type'] == 'p': mu = net.res_trafo.iloc[row['element'], 0] sigma = (abs(mu) * upper_trafo_accuracy - abs(mu) * lower_trafo_accuracy) / 4 elif row['meas_type'] == 'q': mu = net.res_trafo.iloc[row['element'], 1] sigma = (abs(mu) * upper_trafo_accuracy - abs(mu) * lower_trafo_accuracy) / 4 elif row['side'] == 'to': if row['meas_type'] == 'i': mu = net.res_trafo.iloc[row['element'], 7] sigma = (abs(mu) * upper_trafo_accuracy - abs(mu) * lower_trafo_accuracy) / 4 elif row['meas_type'] == 'p': mu = net.res_trafo.iloc[row['element'], 2] sigma = (abs(mu) * upper_trafo_accuracy - abs(mu) * lower_trafo_accuracy) / 4 elif row['meas_type'] == 'q': mu = net.res_trafo.iloc[row['element'], 3] sigma = (abs(mu) * upper_trafo_accuracy - abs(mu) * lower_trafo_accuracy) / 4 # print(mu) value = np.random.normal(mu, sigma, 1) list_value.append(value[0]) list_std.append(sigma) df_measurement['value'] = list_value df_measurement['std_dev'] = list_std for index, row in df_measurement.iterrows(): if row['element_type'] == 'bus': pp.create_measurement(net, row['meas_type'], row['element_type'], value=row['value'], std_dev=row['std_dev'], element=row['element']) elif row['element_type'] == 'line': pp.create_measurement(net, row['meas_type'], row['element_type'], value=row['value'], std_dev=row['std_dev'], element=row['element'], side=row['side']) elif row['element_type'] == 'trafo': if row['meas_type'] in ['p', 'q']: pp.create_measurement(net, row['meas_type'], row['element_type'], value=row['value'], std_dev=row['std_dev'], element=row['element'], side=row['side']) return df_measurement, net
def test_3bus_with_bad_data(): # 1. Create 3-bus-network net = pp.create_empty_network() pp.create_ext_grid(net, 0) pp.create_bus(net, name="bus1", vn_kv=1.) pp.create_bus(net, name="bus2", vn_kv=1.) pp.create_bus(net, name="bus3", vn_kv=1.) pp.create_line_from_parameters(net, 0, 1, 1, r_ohm_per_km=0.7, x_ohm_per_km=0.2, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 0, 2, 1, r_ohm_per_km=0.8, x_ohm_per_km=0.8, c_nf_per_km=0, max_i_ka=1) pp.create_line_from_parameters(net, 1, 2, 1, r_ohm_per_km=1, x_ohm_per_km=0.6, c_nf_per_km=0, max_i_ka=1) pp.create_measurement(net, "p", "line", -0.0011e3, 0.01e3, bus=0, element=0) # Pline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "q", "line", 0.024e3, 0.01e3, bus=0, element=0) # Qline (bus 1 -> bus 2) at bus 1 pp.create_measurement(net, "p", "bus", 0.018e3, 0.01e3, bus=2) # P at bus 3 pp.create_measurement(net, "q", "bus", -0.1e3, 0.01e3, bus=2) # Q at bus 3 pp.create_measurement(net, "v", "bus", 1.08, 0.05, bus=0) # V at bus 1 pp.create_measurement(net, "v", "bus", 1.015, 0.05, bus=2) # V at bus 3 # create false voltage measurement for testing bad data detection (-> should be removed) pp.create_measurement(net, "v", "bus", 1.3, 0.05, bus=1) # V at bus 2 # 2. Do chi2-test success_chi2 = chi2_analysis(net, init='flat') # 3. Perform rn_max_test success_rn_max = remove_bad_data(net, init='flat') v_est_rn_max = net.res_bus_est.vm_pu.values delta_est_rn_max = net.res_bus_est.va_degree.values target_v = np.array([1.0627, 1.0589, 1.0317]) diff_v = target_v - v_est_rn_max target_delta = np.array([0., 0.8677, 3.1381]) diff_delta = target_delta - delta_est_rn_max assert success_chi2 assert success_rn_max assert (np.nanmax(abs(diff_v)) < 1e-4) assert (np.nanmax(abs(diff_delta)) < 1e-4)