def _extract_result_ppci_to_pp(net, ppc, ppci): # convert to pandapower indices ppc = _copy_results_ppci_to_ppc(ppci, ppc, mode="se") # inits empty result tables init_results(net, mode="se") # writes res_bus.vm_pu / va_degree and branch res _extract_results_se(net, ppc) # additionally, write bus power demand results (these are not written in _extract_results) mapping_table = net["_pd2ppc_lookups"]["bus"] net.res_bus_est.index = net.bus.index net.res_bus_est.p_mw = get_values(ppc["bus"][:, 2], net.bus.index.values, mapping_table) net.res_bus_est.q_mvar = get_values(ppc["bus"][:, 3], net.bus.index.values, mapping_table) return net
def _extract_result_ppci_to_pp(net, ppc, ppci): # convert to pandapower indices ppc = _copy_results_ppci_to_ppc(ppci, ppc, mode="se") # extract results from ppc try: _add_pf_options(net, tolerance_mva=1e-8, trafo_loading="current", numba=True, ac=True, algorithm='nr', max_iteration="auto") except: pass # writes res_bus.vm_pu / va_degree and res_line _extract_results_se(net, ppc) # restore backup of previous results _rename_results(net) # additionally, write bus power demand results (these are not written in _extract_results) mapping_table = net["_pd2ppc_lookups"]["bus"] net.res_bus_est.index = net.bus.index net.res_bus_est.p_mw = get_values(ppc["bus"][:, 2], net.bus.index.values, mapping_table) net.res_bus_est.q_mvar = get_values(ppc["bus"][:, 3], net.bus.index.values, mapping_table) _clean_up(net) # delete results which are not correctly calculated for k in list(net.keys()): if k.startswith("res_") and k.endswith("_est") and \ k not in ("res_bus_est", "res_line_est", "res_trafo_est", "res_trafo3w_est"): del net[k] return net
def estimate(self, v_start=None, delta_start=None, calculate_voltage_angles=True): """ The function estimate is the main function of the module. It takes up to three input arguments: v_start, delta_start and calculate_voltage_angles. The first two are the initial state variables for the estimation process. Usually they can be initialized in a "flat-start" condition: All voltages being 1.0 pu and all voltage angles being 0 degrees. In this case, the parameters can be left at their default values (None). If the estimation is applied continuously, using the results from the last estimation as the starting condition for the current estimation can decrease the amount of iterations needed to estimate the current state. The third parameter defines whether all voltage angles are calculated absolutely, including phase shifts from transformers. If only the relative differences between buses are required, this parameter can be set to False. Returned is a boolean value, which is true after a successful estimation and false otherwise. The resulting complex voltage will be written into the pandapower network. The result fields are found res_bus_est of the pandapower network. INPUT: **net** - The net within this line should be created **v_start** (np.array, shape=(1,), optional) - Vector with initial values for all voltage magnitudes in p.u. (sorted by bus index) **delta_start** (np.array, shape=(1,), optional) - Vector with initial values for all voltage angles in degrees (sorted by bus index) OPTIONAL: **calculate_voltage_angles** - (bool) - Take into account absolute voltage angles and phase shifts in transformers Default is True. OUTPUT: **successful** (boolean) - True if the estimation process was successful Optional estimation variables: The bus power injections can be accessed with *se.s_node_powers* and the estimated values corresponding to the (noisy) measurement values with *se.hx*. (*hx* denotes h(x)) EXAMPLE: success = estimate(np.array([1.0, 1.0, 1.0]), np.array([0.0, 0.0, 0.0])) """ if self.net is None: raise UserWarning("Component was not initialized with a network.") t0 = time() # add initial values for V and delta # node voltages # V<delta if v_start is None: v_start = np.ones(self.net.bus.shape[0]) if delta_start is None: delta_start = np.zeros(self.net.bus.shape[0]) # initialize result tables if not existent _copy_power_flow_results(self.net) # initialize ppc ppc, ppci = _init_ppc(self.net, v_start, delta_start, calculate_voltage_angles) # add measurements to ppci structure ppci = _add_measurements_to_ppc(self.net, ppci, self.s_ref) # calculate relevant vectors from ppci measurements z, self.pp_meas_indices, r_cov = _build_measurement_vectors(ppci) # number of nodes n_active = len(np.where(ppci["bus"][:, 1] != 4)[0]) slack_buses = np.where(ppci["bus"][:, 1] == 3)[0] # Check if observability criterion is fulfilled and the state estimation is possible if len(z) < 2 * n_active - 1: self.logger.error("System is not observable (cancelling)") self.logger.error( "Measurements available: %d. Measurements required: %d" % (len(z), 2 * n_active - 1)) return False # set the starting values for all active buses v_m = ppci["bus"][:, 7] delta = ppci["bus"][:, 8] * np.pi / 180 # convert to rad delta_masked = np.ma.array(delta, mask=False) delta_masked.mask[slack_buses] = True non_slack_buses = np.arange(len(delta))[~delta_masked.mask] # matrix calculation object sem = wls_matrix_ops(ppci, slack_buses, non_slack_buses, self.s_ref) # state vector E = np.concatenate((delta_masked.compressed(), v_m)) # invert covariance matrix r_inv = csr_matrix(np.linalg.inv(np.diagflat(r_cov)**2)) current_error = 100. cur_it = 0 G_m, r, H, h_x = None, None, None, None while current_error > self.tolerance and cur_it < self.max_iterations: self.logger.debug(" Starting iteration %d" % (1 + cur_it)) try: # create h(x) for the current iteration h_x = sem.create_hx(v_m, delta) # residual r r = csr_matrix(z - h_x).T # jacobian matrix H H = csr_matrix(sem.create_jacobian(v_m, delta)) # gain matrix G_m # G_m = H^t * R^-1 * H G_m = H.T * (r_inv * H) # state vector difference d_E # d_E = G_m^-1 * (H' * R^-1 * r) d_E = spsolve(G_m, H.T * (r_inv * r)) E += d_E # update V/delta delta[non_slack_buses] = E[:len(non_slack_buses)] v_m = np.squeeze(E[len(non_slack_buses):]) # prepare next iteration cur_it += 1 current_error = np.max(np.abs(d_E)) self.logger.debug("Current error: %.7f" % current_error) except np.linalg.linalg.LinAlgError: self.logger.error( "A problem appeared while using the linear algebra methods." "Check and change the measurement set.") return False # print output for results if current_error <= self.tolerance: successful = True self.logger.debug( "WLS State Estimation successful (%d iterations)" % cur_it) else: successful = False self.logger.debug( "WLS State Estimation not successful (%d/%d iterations)" % (cur_it, self.max_iterations)) # store results for all elements # calculate bus power injections v_cpx = v_m * np.exp(1j * delta) bus_powers_conj = np.zeros(len(v_cpx), dtype=np.complex128) for i in range(len(v_cpx)): bus_powers_conj[i] = np.dot(sem.Y_bus[i, :], v_cpx) * np.conjugate( v_cpx[i]) ppci["bus"][:, 2] = bus_powers_conj.real # saved in per unit ppci["bus"][:, 3] = -bus_powers_conj.imag # saved in per unit ppci["bus"][:, 7] = v_m ppci["bus"][:, 8] = delta * 180 / np.pi # convert to degree # calculate line results (in ppc_i) s_ref, bus, gen, branch = _get_pf_variables_from_ppci(ppci)[0:4] out = np.flatnonzero(branch[:, BR_STATUS] == 0) # out-of-service branches br = np.flatnonzero(branch[:, BR_STATUS]).astype( int) # in-service branches # complex power at "from" bus Sf = v_cpx[np.real(branch[br, F_BUS]).astype(int)] * np.conj( sem.Yf[br, :] * v_cpx) * s_ref # complex power injected at "to" bus St = v_cpx[np.real(branch[br, T_BUS]).astype(int)] * np.conj( sem.Yt[br, :] * v_cpx) * s_ref branch[np.ix_(br, [PF, QF, PT, QT])] = np.c_[Sf.real, Sf.imag, St.real, St.imag] branch[np.ix_(out, [PF, QF, PT, QT])] = np.zeros((len(out), 4)) et = time() - t0 ppci = _store_results_from_pf_in_ppci(ppci, bus, gen, branch, successful, cur_it, et) # convert to pandapower indices ppc = _copy_results_ppci_to_ppc(ppci, ppc, mode="se") # extract results from ppc _add_pf_options(self.net, tolerance_kva=1e-5, trafo_loading="current", numba=True, ac=True, algorithm='nr', max_iteration="auto") # writes res_bus.vm_pu / va_degree and res_line _extract_results_se(self.net, ppc) # restore backup of previous results _rename_results(self.net) # additionally, write bus power injection results (these are not written in _extract_results) mapping_table = self.net["_pd2ppc_lookups"]["bus"] self.net.res_bus_est.p_kw = -get_values( ppc["bus"][:, 2], self.net.bus.index.values, mapping_table) * self.s_ref / 1e3 self.net.res_bus_est.q_kvar = -get_values( ppc["bus"][:, 3], self.net.bus.index.values, mapping_table) * self.s_ref / 1e3 # store variables required for chi^2 and r_N_max test: self.R_inv = r_inv.toarray() self.Gm = G_m.toarray() self.r = r.toarray() self.H = H.toarray() self.Ht = self.H.T self.hx = h_x self.V = v_m self.delta = delta # delete results which are not correctly calculated for k in list(self.net.keys()): if k.startswith("res_") and k.endswith("_est") and \ k not in ("res_bus_est", "res_line_est", "res_trafo_est", "res_trafo3w_est"): del self.net[k] return successful
def estimate(self, v_start='flat', delta_start='flat', calculate_voltage_angles=True, zero_injection=None, fuse_buses_with_bb_switch='all'): """ The function estimate is the main function of the module. It takes up to three input arguments: v_start, delta_start and calculate_voltage_angles. The first two are the initial state variables for the estimation process. Usually they can be initialized in a "flat-start" condition: All voltages being 1.0 pu and all voltage angles being 0 degrees. In this case, the parameters can be left at their default values (None). If the estimation is applied continuously, using the results from the last estimation as the starting condition for the current estimation can decrease the amount of iterations needed to estimate the current state. The third parameter defines whether all voltage angles are calculated absolutely, including phase shifts from transformers. If only the relative differences between buses are required, this parameter can be set to False. Returned is a boolean value, which is true after a successful estimation and false otherwise. The resulting complex voltage will be written into the pandapower network. The result fields are found res_bus_est of the pandapower network. INPUT: **net** - The net within this line should be created **v_start** (np.array, shape=(1,), optional) - Vector with initial values for all voltage magnitudes in p.u. (sorted by bus index) **delta_start** (np.array, shape=(1,), optional) - Vector with initial values for all voltage angles in degrees (sorted by bus index) OPTIONAL: **calculate_voltage_angles** - (bool) - Take into account absolute voltage angles and phase shifts in transformers Default is True **zero_injection** - (str, iterable, None) - Defines which buses are zero injection bus or the method to identify zero injection bus, with 'wls_estimator' virtual measurements will be added, with 'wls_estimator with zero constraints' the buses will be handled as constraints "auto": all bus without p,q measurement, without p, q value (load, sgen...) and aux buses will be identified as zero injection bus "aux_bus": only aux bus will be identified as zero injection bus None: no bus will be identified as zero injection bus iterable: the iterable should contain index of the zero injection bus and also aux bus will be identified as zero-injection bus **fuse_buses_with_bb_switch** - (str, iterable, None) - Defines how buses with closed bb switches should be handled, if fuse buses will only fused to one for calculation, if not fuse, an auxiliary bus and auxiliary line will be automatically added to the network to make the buses with different p,q injection measurements identifieble "all": all buses with bb-switches will be fused, the same as the default behaviour in load flow None: buses with bb-switches and individual p,q measurements will be reconfigurated by auxiliary elements iterable: the iterable should contain the index of buses to be fused, the behaviour is contigous e.g. if one of the bus among the buses connected through bb switch is given, then all of them will still be fused OUTPUT: **successful** (boolean) - True if the estimation process was successful Optional estimation variables: The bus power injections can be accessed with *se.s_node_powers* and the estimated values corresponding to the (noisy) measurement values with *se.hx*. (*hx* denotes h(x)) EXAMPLE: success = estimate(np.array([1.0, 1.0, 1.0]), np.array([0.0, 0.0, 0.0])) """ if self.net is None: raise UserWarning("Component was not initialized with a network.") t0 = time() # change the configuration of the pp net to avoid auto fusing of buses connected # through bb switch with elements on each bus if this feature enabled bus_to_be_fused = None if fuse_buses_with_bb_switch != 'all' and not self.net.switch.empty: if isinstance(fuse_buses_with_bb_switch, str): raise UserWarning( "fuse_buses_with_bb_switch parameter is not correctly initialized" ) elif hasattr(fuse_buses_with_bb_switch, '__iter__'): bus_to_be_fused = fuse_buses_with_bb_switch _add_aux_elements_for_bb_switch(self.net, bus_to_be_fused) # add initial values for V and delta # node voltages # V<delta if v_start is None: v_start = "flat" if delta_start is None: delta_start = "flat" # initialize result tables if not existent _copy_power_flow_results(self.net) # initialize ppc ppc, ppci = _init_ppc(self.net, v_start, delta_start, calculate_voltage_angles) # add measurements to ppci structure ppci = _add_measurements_to_ppc(self.net, ppci, zero_injection) # Finished converting pandapower network to ppci # Estimate voltage magnitude and angle with the given estimator delta, v_m = self.estimator.estimate(ppci) # store results for all elements # calculate bus power injections v_cpx = v_m * np.exp(1j * delta) bus_powers_conj = np.zeros(len(v_cpx), dtype=np.complex128) for i in range(len(v_cpx)): bus_powers_conj[i] = np.dot(ppci['internal']['Y_bus'][i, :], v_cpx) * np.conjugate(v_cpx[i]) ppci["bus"][:, 2] = bus_powers_conj.real # saved in per unit ppci["bus"][:, 3] = -bus_powers_conj.imag # saved in per unit ppci["bus"][:, 7] = v_m ppci["bus"][:, 8] = delta * 180 / np.pi # convert to degree # calculate line results (in ppc_i) s_ref, bus, gen, branch = _get_pf_variables_from_ppci(ppci)[0:4] out = np.flatnonzero(branch[:, BR_STATUS] == 0) # out-of-service branches br = np.flatnonzero(branch[:, BR_STATUS]).astype( int) # in-service branches # complex power at "from" bus Sf = v_cpx[np.real(branch[br, F_BUS]).astype(int)] * np.conj( ppci['internal']['Yf'][br, :] * v_cpx) * s_ref # complex power injected at "to" bus St = v_cpx[np.real(branch[br, T_BUS]).astype(int)] * np.conj( ppci['internal']['Yt'][br, :] * v_cpx) * s_ref branch[np.ix_(br, [PF, QF, PT, QT])] = np.c_[Sf.real, Sf.imag, St.real, St.imag] branch[np.ix_(out, [PF, QF, PT, QT])] = np.zeros((len(out), 4)) et = time() - t0 ppci = _store_results_from_pf_in_ppci(ppci, bus, gen, branch, self.estimator.successful, self.estimator.iterations, et) # convert to pandapower indices ppc = _copy_results_ppci_to_ppc(ppci, ppc, mode="se") # extract results from ppc _add_pf_options(self.net, tolerance_mva=1e-8, trafo_loading="current", numba=True, ac=True, algorithm='nr', max_iteration="auto") # writes res_bus.vm_pu / va_degree and res_line _extract_results_se(self.net, ppc) # restore backup of previous results _rename_results(self.net) # additionally, write bus power injection results (these are not written in _extract_results) mapping_table = self.net["_pd2ppc_lookups"]["bus"] self.net.res_bus_est.p_mw = -get_values( ppc["bus"][:, 2], self.net.bus.index.values, mapping_table) self.net.res_bus_est.q_mvar = -get_values( ppc["bus"][:, 3], self.net.bus.index.values, mapping_table) _clean_up(self.net) # clear the aux elements and calculation results created for the substitution of bb switches if fuse_buses_with_bb_switch != 'all' and not self.net.switch.empty: _drop_aux_elements_for_bb_switch(self.net) # delete results which are not correctly calculated for k in list(self.net.keys()): if k.startswith("res_") and k.endswith("_est") and \ k not in ("res_bus_est", "res_line_est", "res_trafo_est", "res_trafo3w_est"): del self.net[k] return self.estimator.successful