def test_to_ppc_and_mpc(): # pypower cases to validate functions = [ 'case4gs', 'case6ww', 'case14', 'case30', 'case24_ieee_rts', 'case39' ] for fn in functions: # get pypower results pypower_module = __import__('pypower.' + fn) pypower_submodule = getattr(pypower_module, fn) pypower_function = getattr(pypower_submodule, fn) ppc_net = pypower_function() # get net from pandapower res_pypower, status_pypower = runpf(ppc_net, ppopt=ppoption(VERBOSE=0, OUT_ALL=0)) pandapower_module = __import__('pandapower', fromlist=['networks']) pandapower_function = getattr(pandapower_module.networks, fn) net = pandapower_function() reset_results(net) # convert to ppc ppc = cv.to_ppc(net) # convert to mpc mpc = cv.to_mpc(net) # runpf from converted ppc res_converted_pp, status_converted_pp = runpf( ppc, ppopt=ppoption(VERBOSE=0, OUT_ALL=0)) if status_converted_pp and status_pypower: # get lookup pp2ppc bus_lookup = net["_pd2ppc_lookups"]["bus"] # check for equality in bus voltages pp_buses = bus_lookup[res_converted_pp['bus'][:, BUS_I].astype(int)] assert np.allclose(res_converted_pp['bus'][pp_buses, VM:VA + 1], res_pypower['bus'][:, VM:VA + 1]) # ToDo: check equality of branch and gen values # pp_gen = bus_lookup[res_converted_pp['bus'][:, BUS_I].astype(int)] # assert np.allclose(res_pypower['gen'][res_pypower['order']['gen']['e2i'], PG:QG+1] # , res_converted_pp['gen'][:, PG:QG+1]) else: raise LoadflowNotConverged("Loadflow did not converge!")
def test_to_ppc_and_mpc(): # pypower cases to validate functions = ['case4gs', 'case6ww', 'case30', 'case39'] for fn in functions: # get pypower grids with results ppc_net = get_testgrids(fn, 'pypower_cases.p') # get pandapower grids pandapower_module = __import__('pandapower', fromlist=['networks']) pandapower_function = getattr(pandapower_module.networks, fn) net = pandapower_function() reset_results(net) # convert pandapower grids to ppc ppc = cv.to_ppc(net) # convert pandapower grids to mpc (no result validation) mpc = cv.to_mpc(net) # validate voltage results of pandapower-to-ppc-converted grids vs. original pypower results net["_options"]['ac'] = True net["_options"]['numba'] = True net["_options"]['tolerance_mva'] = 1e-8 net["_options"]['algorithm'] = "fdbx" net["_options"]['max_iteration'] = 30 net["_options"]['enforce_q_lims'] = False net["_options"]['calculate_voltage_angles'] = True res_converted_pp, status_converted_pp = _runpf_pypower( ppc, net["_options"]) if status_converted_pp: # get lookup pp2ppc bus_lookup = net["_pd2ppc_lookups"]["bus"] # check for equality in bus voltages pp_buses = bus_lookup[res_converted_pp['bus'][:, BUS_I].astype(int)] res1 = res_converted_pp['bus'][pp_buses, VM:VA + 1] res2 = ppc_net['bus'][:, VM:VA + 1] assert np.allclose(res1, res2) else: raise LoadflowNotConverged("Loadflow did not converge!")
def to_mpc(net, filename=None, init="results", calculate_voltage_angles=False, trafo_model="t", mode="pf"): """ This function converts a pandapower net to a matpower case files (.mat) version 2. Note: python is 0-based while Matlab is 1-based. INPUT: **net** - The pandapower net. OPTIONAL: **filename** (None) - File path + name of the mat file which will be created. If None the mpc will only be returned **init** (str, "results") - initialization method of the loadflow For the conversion to a mpc, the following options can be chosen: - "flat"- flat start with voltage of 1.0pu and angle of 0° at all buses as initial solution - "results" - voltage vector of last loadflow from net.res_bus is copied to the mpc **calculate_voltage_angles** (bool, False) - copy the voltage angles from pandapower to the mpc If True, voltage angles are copied from pandapower to the mpc. In some cases with large differences in voltage angles (for example in case of transformers with high voltage shift), the difference between starting and end angle value is very large. In this case, the loadflow might be slow or it might not converge at all. That is why the possibility of neglecting the voltage angles of transformers and ext_grids is provided to allow and/or accelarate convergence for networks where calculation of voltage angles is not necessary. The default value is False because pandapower was developed for distribution networks. Please be aware that this parameter has to be set to True in meshed network for correct results! **trafo_model** (str, "t") - transformer equivalent circuit model pandapower provides two equivalent circuit models for the transformer: - "t" - transformer is modelled as equivalent with the T-model. This is consistent with PowerFactory and is also more accurate than the PI-model. We recommend using this transformer model. - "pi" - transformer is modelled as equivalent PI-model. This is consistent with Sincal, but the method is questionable since the transformer is physically T-shaped. We therefore recommend the use of the T-model. EXAMPLE: import pandapower.converter as pc import pandapower.networks as pn net = pn.case9() pc.to_mpc(net) """ # convert to matpower net["converged"] = False if not init == "results": reset_results(net) # select elements in service (time consuming, so we do it once) _get_std_options(net, init, calculate_voltage_angles, trafo_model) net["_options"]["mode"] = mode if mode == "opf": net["_options"]["copy_constraints_to_ppc"] = True # convert pandapower net to ppc ppc, ppci = _pd2ppc(net) # convert ppc to mpc if mode == "opf": ppc["gencost"] = ppci["gencost"] mpc = _ppc_to_mpc(ppc) if filename is not None: # savemat savemat(filename, mpc) return mpc