def test_PTDF(): net = nw.case9() pp.rundcpp(net) _, ppci = _pd2ppc(net) ptdf = makePTDF(ppci["baseMVA"], ppci["bus"], ppci["branch"], using_sparse_solver=False) _ = makePTDF(ppci["baseMVA"], ppci["bus"], ppci["branch"], result_side=1, using_sparse_solver=False) ptdf_sparse = makePTDF(ppci["baseMVA"], ppci["bus"], ppci["branch"], using_sparse_solver=True) if not np.allclose(ptdf, ptdf_sparse): raise AssertionError( "Sparse PTDF has differenct result against dense PTDF") if not ptdf.shape == (ppci["bus"].shape[0], ppci["branch"].shape[0]): raise AssertionError("PTDF has wrong dimension") if not np.all(~np.isnan(ptdf)): raise AssertionError("PTDF has NaN value")
def test_pmu_case14(): net = nw.case9() pp.runpp(net) add_virtual_pmu_meas_from_loadflow(net) run_se_lp_verify(net)
def test_replace_gen_sgen(): net = nw.case9() vm_set = [1.03, 1.02] net.gen["vm_pu"] = vm_set pp.runpp(net) assert list(net.res_gen.index.values) == [0, 1] # replace_gen_by_sgen pp.replace_gen_by_sgen(net) assert not net.gen.shape[0] assert not net.res_gen.shape[0] assert not np.allclose(net.sgen.q_mvar.values, 0) assert net.res_gen.shape[0] == 0 pp.runpp(net) assert np.allclose(net.res_bus.loc[net.sgen.bus, "vm_pu"].values, vm_set) # replace_sgen_by_gen net2 = copy.deepcopy(net) pp.replace_sgen_by_gen(net2, [1]) assert net2.gen.shape[0] == 1 assert net2.res_gen.shape[0] == 1 assert net2.gen.shape[0] == 1 assert net2.res_gen.shape[0] == 1 pp.replace_sgen_by_gen(net, 1) assert pp.nets_equal(net, net2)
def main(): net = ppnw.case9() pp.runpp(net) # Make sure network contains ybus and the solution values. print(net) p_slack = run_lf(net) print(f"Slack generator power output: {p_slack}")
def test_case9(): net = pn.case9() assert net.converged pp.runpp(net, trafo_model='pi') assert len(net.bus) == 9 assert len(net.line) + len(net.trafo) == 9 assert len(net.ext_grid) + len(net.gen) + len(net.sgen) == 3 assert net.converged
def test_LODF(): net = nw.case9() pp.rundcpp(net) _, ppci = _pd2ppc(net) ptdf = makePTDF(ppci["baseMVA"], ppci["bus"], ppci["branch"]) lodf = makeLODF(ppci["branch"], ptdf) if not lodf.shape == (ppci["branch"].shape[0], ppci["branch"].shape[0]): raise AssertionError("LODF has wrong dimension")
def test_clear_result_tables(): net = nw.case9() pp.runpp(net) elms_to_check = ["bus", "line", "load"] for elm in elms_to_check: assert net["res_%s" % elm].shape[0] pp.clear_result_tables(net) for elm in elms_to_check: assert not net["res_%s" % elm].shape[0]
def test_lp_lav(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) estimate(net, algorithm="lp") if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or\ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_lp_lav(): ''' This will test the default LP solver installed. If OR-Tools is installed, it will use it. Otherwise scipy is used. ''' net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) estimate(net, algorithm="lp") if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_ql_qc(): net = nw.case9() net.sn_mva = 1. pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) pf_vm_pu, pf_va_degree = net.res_bus.vm_pu, net.res_bus.va_degree # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="ql", a=3, verbose=False) if not estimation_opt.successful: eppci = estimation_opt.estimate(eppci, estimator="ql", a=3, opt_method="Newton-CG", verbose=False) if not estimation_opt.successful: raise AssertionError("Estimation failed due to algorithm failing!") net = eppci2pp(net, ppc, eppci) if not np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!") # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-6, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="qc", a=3, verbose=False) if not estimation_opt.successful: eppci = estimation_opt.estimate(eppci, estimator="qc", a=3, opt_method="Newton-CG", verbose=False) net = eppci2pp(net, ppc, eppci) if not np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_net_oos_line(): net = nw.case9() net.line.in_service.iat[4] = False pp.runpp(net) for line_ix in net.line.index: pp.create_measurement(net, "p", "line", net.res_line.at[line_ix, "p_from_mw"], 0.01, element=line_ix, side="from") pp.create_measurement(net, "q", "line", net.res_line.at[line_ix, "q_from_mvar"], 0.01, element=line_ix, side="from") for bus_ix in net.bus.index: pp.create_measurement(net, "v", "bus", net.res_bus.at[bus_ix, "vm_pu"], 0.01, element=bus_ix) if not estimate(net, tolerance=1e-6, zero_injection=None): raise AssertionError("Estimation failed!")
def test_opt_lav(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, with_random_error=False) net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 1000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="lav", verbose=False) net = eppci2pp(net, ppc, eppci) if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_ql_qc(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, p_std_dev=0.01, q_std_dev=0.01) pf_vm_pu, pf_va_degree = net.res_bus.vm_pu, net.res_bus.va_degree # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) try: eppci = estimation_opt.estimate(eppci, estimator="ql", a=3) assert estimation_opt.successful except: eppci = estimation_opt.estimate(eppci, estimator="ql", a=3, opt_method="Newton-CG") assert estimation_opt.successful net = eppci2pp(net, ppc, eppci) assert np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2) # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-6, 5) estimation_opt = OptAlgorithm(1e-6, 3000) eppci = estimation_wls.estimate(eppci) try: eppci = estimation_opt.estimate(eppci, estimator="qc", a=3) assert estimation_opt.successful except: eppci = estimation_opt.estimate(eppci, estimator="qc", a=3, opt_method="Newton-CG") assert estimation_opt.successful net = eppci2pp(net, ppc, eppci) assert np.allclose(pf_vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(pf_va_degree, net.res_bus_est.va_degree, atol=5e-2)
def test_opt_lav(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, with_random_error=False) net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 5) estimation_opt = OptAlgorithm(1e-6, 1000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="lav") assert estimation_opt.successful net = eppci2pp(net, ppc, eppci) assert np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) assert np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2)
def test_case9_conversion(): net = pn.case9() ppc = to_ppc(net) # correction because to_ppc do net export max_loading_percent: ppc["branch"][:, 5] = [250, 250, 150, 300, 150] + [250] * 4 # correction because voltage limits are set to 1.0 at slack buses ppc["bus"][0, 11] = 0.9 ppc["bus"][0, 12] = 1.1 net2 = from_ppc(ppc) pp.runpp(net) pp.runpp(net2) assert pp.nets_equal(net, net2, check_only_results=True, tol=1e-10) pp.runopp(net) pp.runopp(net2) assert pp.nets_equal(net, net2, check_only_results=True, tol=1e-10)
def test_case9(): """ basic test with ext_grid + gen, scaling != 1, slack_weight sum = 1 """ tol_mw = 1e-6 net = networks.case9() # net = case9_simplified() # set slack_weight (distributed slack participation factor) net.ext_grid['slack_weight'] = 1 / 3 net.gen['slack_weight'] = 1 / 3 # todo: is it clearer to consider scaling or to ignore it? right now is ignored # net.gen["scaling"] = [0.8, 0.7] net.gen["scaling"] = [1, 1] # # set ext_grid dispatched active power # net.ext_grid['p_disp_mw'] = 30 run_and_assert_numba(net) # active power difference of dispatched and result ext_grid_diff_p = 0 - net.res_ext_grid.p_mw gen_diff = net.gen.p_mw * net.gen.scaling - net.res_gen.p_mw # resulting active slack power res_p_slack = ext_grid_diff_p.sum() + gen_diff.sum() # calculate target active power difference p_target_ext_grid = res_p_slack * net.ext_grid.slack_weight p_target_gen = res_p_slack * net.gen.slack_weight # check the power balances assert np.allclose(ext_grid_diff_p, p_target_ext_grid, atol=tol_mw) assert np.allclose(gen_diff, p_target_gen, atol=tol_mw) # check balance of power injected_p_mw, consumed_p_mw, xward_p_mw = _get_injection_consumption(net) assert abs(net.res_ext_grid.p_mw.sum() + net.res_gen.p_mw.sum() - consumed_p_mw - xward_p_mw) < 1e-6 # check the distribution formula of the slack power difference assert_results_correct(net, tol=1e-6)
def test_replace_gen_sgen(): for i in range(2): net = nw.case9() vm_set = [1.03, 1.02] net.gen["vm_pu"] = vm_set net.gen["dspf"] = 1 pp.runpp(net) assert list(net.res_gen.index.values) == [0, 1] # replace_gen_by_sgen if i == 0: pp.replace_gen_by_sgen(net) elif i == 1: pp.replace_gen_by_sgen(net, [0, 1], sgen_indices=[4, 1], cols_to_keep=[ "max_p_mw"], add_cols_to_keep=["dspf"]) # min_p_mw is not in cols_to_keep assert np.allclose(net.sgen.index.values, [4, 1]) assert np.allclose(net.sgen.dspf.values, 1) assert "max_p_mw" in net.sgen.columns assert "min_p_mw" not in net.sgen.columns assert not net.gen.shape[0] assert not net.res_gen.shape[0] assert not np.allclose(net.sgen.q_mvar.values, 0) assert net.res_gen.shape[0] == 0 pp.runpp(net) assert np.allclose(net.res_bus.loc[net.sgen.bus, "vm_pu"].values, vm_set) # replace_sgen_by_gen net2 = copy.deepcopy(net) if i == 0: pp.replace_sgen_by_gen(net2, [1]) elif i == 1: pp.replace_sgen_by_gen(net2, 1, gen_indices=[2], add_cols_to_keep=["dspf"]) assert np.allclose(net2.gen.index.values, [2]) assert np.allclose(net2.gen.dspf.values, 1) assert net2.gen.shape[0] == 1 assert net2.res_gen.shape[0] == 1 assert net2.gen.shape[0] == 1 assert net2.res_gen.shape[0] == 1 if i == 0: pp.replace_sgen_by_gen(net, 1) assert pp.nets_equal(net, net2)
def get_net(): """ Get a pp network consistent with pypower dynamic nine bus case. """ net = nw.case9() # Make consistent with the case in pypower dynamics. net.sn_mva = 100.0 net.gen.vm_pu = 1.025 net.ext_grid.vm_pu = 1.04 pp.create_continuous_bus_index(net, 1) pp.reindex_buses(net, { 5: 6, 6: 9, 7: 8, 8: 7, 9: 5, }) net.bus = net.bus.sort_index() net.load = net.load.sort_values('bus').reset_index(drop=True) net.line = net.line.sort_values('from_bus').reset_index(drop=True) return net
def test_lp_ortools_lav(): ''' If OR-Tools is installed, run this test. ''' # Set the solver LPAlgorithm.ortools_available = True net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net, with_random_error=False) net, ppc, eppci = pp2eppci(net) estimation_ortools_lp = LPAlgorithm(1e-3, 5) estimation_ortools = estimation_ortools_lp.estimate(eppci, with_ortools=True) net = eppci2pp(net, ppc, eppci) if not np.allclose(net.res_bus.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net.res_bus.va_degree, net.res_bus_est.va_degree, atol=5e-2): raise AssertionError("Estimation failed!")
def test_case9_conversion(): net = pn.case9() # set max_loading_percent to enable line limit conversion net.line["max_loading_percent"] = 100 ppc = to_ppc(net, mode="opf") # correction because voltage limits are set to 1.0 at slack buses ppc["bus"][0, 12] = 0.9 ppc["bus"][0, 11] = 1.1 net2 = from_ppc(ppc, f_hz=net.f_hz) # again add max_loading_percent to enable valid comparison net2.line["max_loading_percent"] = 100 # compare loadflow results pp.runpp(net) pp.runpp(net2) assert pp.nets_equal(net, net2, check_only_results=True, tol=1e-10) # compare optimal powerflow results pp.runopp(net) pp.runopp(net2) assert pp.nets_equal(net, net2, check_only_results=True, tol=1e-10)
def test_case9_compare_classical_wls_opt_wls(): net = nw.case9() pp.runpp(net) add_virtual_meas_from_loadflow(net) # give it a warm start net, ppc, eppci = pp2eppci(net) estimation_wls = WLSAlgorithm(1e-3, 3) estimation_opt = OptAlgorithm(1e-6, 1000) eppci = estimation_wls.estimate(eppci) eppci = estimation_opt.estimate(eppci, estimator="wls", verbose=False) if not estimation_opt.successful: raise AssertionError("Estimation failed due to algorithm failing!") net = eppci2pp(net, ppc, eppci) net_wls = net.deepcopy() estimate(net_wls) if not np.allclose(net_wls.res_bus_est.vm_pu, net.res_bus_est.vm_pu, atol=1e-2) or \ not np.allclose(net_wls.res_bus_est.va_degree, net.res_bus_est.va_degree, atol=1e-2): raise AssertionError("Estimation failed!")
def main(): net = ppnw.case9() pp.runpp(net) # Make sure network contains ybus and the solution values. load_p = np.zeros((net.bus.shape[0], ), np.float32) print(net) p_slack = run_lf(load_p, net) print(f'Slack generator power output: {p_slack}') print(f'\nGradient of slack power with respect to load at each bus:') t1 = time.perf_counter() f_grad_p_slack = grad(lambda x: run_lf(x, net)) grad_p_slack = f_grad_p_slack(load_p) print(f'\tUsing this program with autograd:\n{grad_p_slack}') print(f'Took {time.perf_counter() - t1:.2f} Seconds') t1 = time.perf_counter() grad_p_slack_fin_diff = fin_diff(lambda x: run_lf(x, net), load_p) print(f'\tUsing this program finite differences:\n{grad_p_slack_fin_diff}') print(f'Took {time.perf_counter() - t1:.2f} Seconds') t1 = time.perf_counter() grad_p_slack_fin_diff_pp = fin_diff(lambda x: run_lf_pp(x, net, 'nr'), load_p) print( f'\nCalculated using finite differences and pandapower newton raphson' f':\n{grad_p_slack_fin_diff_pp}') print(f'Took {time.perf_counter() - t1:.2f} Seconds') t1 = time.perf_counter() grad_p_slack_fin_diff_pp = fin_diff(lambda x: run_lf_pp(x, net, 'gs'), load_p) print(f'\nCalculated using finite differences and pandapower gauss siedel' f':\n{grad_p_slack_fin_diff_pp}') print(f'Took {time.perf_counter() - t1:.2f} Seconds')
def test_case9(): net = pn.case9() assert net.converged _ppc_element_test(net, 9, 9, 3, True)
# -*- coding: utf-8 -*- """ Created on Sat Nov 30 12:36:20 2019 @author: digvijaygusain """ import numpy as np, pandas as pd, pandapower as pp, pandapower.networks as pn from MVMO import MVMO net = pn.case9() pp.runopp(net, init='pf') cost_pp = net.res_cost cost = net.poly_cost.iloc[:, [2, 3, 4]] def function(x): net.gen.iloc[:, 4] = x try: pp.runpp(net) gen = net.res_ext_grid.iloc[:, 0].tolist() + net.res_gen.iloc[:, 0].tolist() return abs( sum(cost.iloc[:, 0] + pd.Series(gen) * (cost.iloc[:, 1]) + (pd.Series(gen)**2) * (cost.iloc[:, 2]))) except: return False